我正在尝试读取视觉基础知识中的文本文件,但我无法将详细信息存储在数组中,它会出现错误

罗尼尔·库马尔
Dim EmpId(100) As String
Dim Lastname(100) As String
Dim firstname(100) As String
Dim hrs(100) As Integer
Dim min(100) As Integer
Dim counter As Integer
Dim i As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'declare variables  
    Dim result As DialogResult 'what did the user choose in the open dialog (open or cancel)
    Dim strFileName As String 'store file path of the selected file
    Dim strRecord As String
    Dim sreStreamReader As IO.StreamReader
    Dim strFieldValues(100) As String 'to store extracted field values.

    Try
        result = filebrowser.ShowDialog
        If result = Windows.Forms.DialogResult.OK Then
            strFileName = filebrowser.FileName
            'create a StreamReader object by opening the file for input
            sreStreamReader = IO.File.OpenText(strFileName)

            counter = 0
            i = 0
            Do While sreStreamReader.Peek() <> -1
                strRecord = sreStreamReader.ReadLine()
                strFieldValues = strRecord.Split(",")
                EmpId(counter) = strFieldValues(i) & firstname(counter) = strFieldValues(i) & Lastname(counter) = strFieldValues(i) _
                & hrs(counter) = strFieldValues(i) & min(counter) = strFieldValues(i)
                counter = counter + 1
                i = i + 1
            Loop
            Dim j As Integer
            For j = 0 To j < counter Step +1
                Label1.Text = EmpId(j) & firstname(j) & Lastname(j) & hrs(j) & min(j)
            Next





        sreStreamReader.Close()
        End If
    Catch exFile As IO.FileNotFoundException
        'processed when the file cannot be found            
        MessageBox.Show("Cannot locate the file.", _
            "FileopenDialog", MessageBoxButtons.OK, _
            MessageBoxIcon.Information)
        '  Catch ex As Exception
        'handles any other errors            
        '   MessageBox.Show(ex.Message, "FileopenDialog", _
        '    MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Try
玛丽

我假设如果在记事本中打开您的文本文件将如下所示。

1,Smith,Mary,7,45
2,Jones,Bill, 8,15
3,Mathew,Mark,6, 20
4,Luke,John,9,30

注意没有空格。如果有空格,您可能必须在字符串上使用 .Trim。

我有一个名为Employee. 在类中有一个自定义构造函数 (the Sub New),它设置所有适当的属性。这些值将来自文本文件,您将在一分钟内看到。

然后在按钮单击事件Employee中创建类型列表

Dim EmpList As New List(Of Employee)

它可以保存 Employee 类型的对象。我使用 `List(0f T) 而不是数组,因为我不必提前知道文件中有多少行。

对象.ReadAllLines方法File返回一个数组,其中包含文件中的每一行作为数组的元素。循环遍历行数组,.Split在每一行上调用它返回一个数组,大概包含您感兴趣的每个字段。每个元素都被发送到 Employee 的构造函数,并且生成的新 Employee 被添加到列表中。如果文件中有多余的空格,您可能需要在此处调用.Trim以删除它们

最后,我在设计时向表单添加了一个 DataGridView。将 .DataSource 属性设置为员工列表,您的数据将显示在带有标题的网格中。

Public Class Employee
    Public Property ID As String
    Public Property LasName As String
    Public Property FirstName As String
    Public Property Hours As Integer
    Public Property Minutes As Integer

    Public Sub New(iden As String, lname As String, fname As String, hr As Integer, mn As Integer)
        ID = iden
        LasName = lname
        FirstName = fname
        Hours = hr
        Minutes = mn
    End Sub
End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim EmpList As New List(Of Employee)

    Try
        Dim result = OpenFileDialog1.ShowDialog
        If Not result = Windows.Forms.DialogResult.OK Then
            MessageBox.Show("No file selected")
        End If
        Dim strFileName = OpenFileDialog1.FileName

        Dim lines = IO.File.ReadAllLines(strFileName)
        For Each line In lines
            Dim strFieldValues = line.Split(","c)
            EmpList.Add(New Employee(strFieldValues(0), strFieldValues(1), strFieldValues(2), CInt(strFieldValues(3)), CInt(strFieldValues(4))))
        Next
        DataGridView1.DataSource = EmpList
    Catch exFile As IO.FileNotFoundException
        MessageBox.Show("Cannot locate the file.", "FileopenDialog", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Try
End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章