我正在尝试读取 visual basics 中的文本文件,但我无法将详细信息存储在它给出错误的数组中
i am trying to read a text file in visual basics but i am not able to store the details in the array it gives errors
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
的 class。在 class 中有一个自定义构造函数(Sub New
),它设置所有适当的属性。正如您将在一分钟内看到的那样,这些值将来自文本文件。
然后在按钮单击事件中创建类型 Employee
的列表。
Dim EmpList As New List(Of Employee)
它可以容纳 object 类型的 Employee。我使用 `List(0f T) 而不是数组,因为我不必提前知道文件中有多少行。
.ReadAllLines
方法 File
object returns 一个包含文件中每一行的数组作为数组的一个元素。在每一行上循环调用 .Split
的行数组,其中 returns 可能包含您感兴趣的每个字段的数组。每个元素都被发送到 Employee 的构造函数,并将生成的新 Employee 添加到名单。如果文件中有多余的空格,您可能需要调用 .Trim
来删除它们
最后,我在设计时向窗体添加了一个 DataGridView。将 .DataSource 属性 设置为员工列表,您的数据将显示在带有 headers.
的网格中
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
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
的 class。在 class 中有一个自定义构造函数(Sub New
),它设置所有适当的属性。正如您将在一分钟内看到的那样,这些值将来自文本文件。
然后在按钮单击事件中创建类型 Employee
的列表。
Dim EmpList As New List(Of Employee)
它可以容纳 object 类型的 Employee。我使用 `List(0f T) 而不是数组,因为我不必提前知道文件中有多少行。
.ReadAllLines
方法 File
object returns 一个包含文件中每一行的数组作为数组的一个元素。在每一行上循环调用 .Split
的行数组,其中 returns 可能包含您感兴趣的每个字段的数组。每个元素都被发送到 Employee 的构造函数,并将生成的新 Employee 添加到名单。如果文件中有多余的空格,您可能需要调用 .Trim
来删除它们
最后,我在设计时向窗体添加了一个 DataGridView。将 .DataSource 属性 设置为员工列表,您的数据将显示在带有 headers.
的网格中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