从文本文件中每隔四行读取一次
Read every fourth line from a text file
所以,我有一个包含以下内容的文本文件:
Andrew Law
0276376352
13 Parsons St
Kevin Kyle
0376458374
29 Penrod Drive
Billy Madison
06756355
16 Stafford Street
现在在我的表单上有一个列表框。加载表单时,我想从文本文件(每个名称)中每隔四行读取一次并将其显示在列表框中。
我现在只有以下内容:
Dim People As String
People = System.IO.File.ReadAllLines("filelocation")(3)
ListBox1.Items.Add(People)
然而,这只读取第 4 行,因为我也想读取之后的每第四行。
当当前行是要跳过的预定义行数的倍数或 0
时,将从源文件中提取的所有字符串添加到列表框并允许用户 select 列表中的名称,用与 selected 名称相关的详细信息填充一些标签。
- 从字符串数组中解析并提取人名。每个名称都可以在索引中找到,该索引是
skipLines
字段指定值的倍数。
- 将每个名称添加到 ListBox 控件。
- 当从名称列表框列表中 select 编辑名称时,将相关详细信息添加到一些标签(名为
lblPhoneNumber
和 lblAddress
这里)。为了识别数组中的正确信息,这次我们使用 skipLines
值作为乘数。这样,即使您在名称列表中添加了更多详细信息,您也会发现仅修改 skipLines
值的正确信息。
您需要订阅ListBox的SelectedIndexChanged事件:
Public Class Form1
Private people As String()
Private skipLines As Integer = 0
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
skipLines = 3
people = File.ReadAllLines([Source File])
For line As Integer = 0 To people.Length - 1
If line Mod skipLines = 0 Then
ListBox1.Items.Add(people(line))
End If
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
Dim StartIndex As Integer = ListBox1.SelectedIndex * skipLines
lblPhoneNumber.Text = people(StartIndex + 1)
lblAddress.Text = people(StartIndex + 2)
End Sub
End Class
这是如何工作的:
定义要跳过的行数。我们想要一行文字,那么跳过3行,这里:
Dim skipLines As Integer = 3
我们创建一个字符串数组。它将包含 File.ReadAllLines() 的输出,当然 returns 一个字符串数组:
Dim people As String() = File.ReadAllLines([Source File])
逐行迭代字符串数组的全部内容。一个集合枚举是从0
开始的,所以我们解析列表从0
到元素个数- 1
:
For line As Integer = 0 To people.Length - 1
'[...]
Next
当当前行号是skipLines
的倍数时满足If
条件。
Mod operator 将两个数相除,returns 运算的余数。如果没有提醒,line
数字是 skipLines
的倍数,我们要跳过的行数。
If line Mod skipLines = 0 Then
`[...]
End If
最后在满足条件的情况下,在当前[=49]代表的索引处添加字符串数组(people
数组的内容=]line
值,到ListBox.Items
集合:
ListBox1.Items.Add(people(line))
所以,我有一个包含以下内容的文本文件:
Andrew Law
0276376352
13 Parsons St
Kevin Kyle
0376458374
29 Penrod Drive
Billy Madison
06756355
16 Stafford Street
现在在我的表单上有一个列表框。加载表单时,我想从文本文件(每个名称)中每隔四行读取一次并将其显示在列表框中。
我现在只有以下内容:
Dim People As String
People = System.IO.File.ReadAllLines("filelocation")(3)
ListBox1.Items.Add(People)
然而,这只读取第 4 行,因为我也想读取之后的每第四行。
当当前行是要跳过的预定义行数的倍数或 0
时,将从源文件中提取的所有字符串添加到列表框并允许用户 select 列表中的名称,用与 selected 名称相关的详细信息填充一些标签。
- 从字符串数组中解析并提取人名。每个名称都可以在索引中找到,该索引是
skipLines
字段指定值的倍数。 - 将每个名称添加到 ListBox 控件。
- 当从名称列表框列表中 select 编辑名称时,将相关详细信息添加到一些标签(名为
lblPhoneNumber
和lblAddress
这里)。为了识别数组中的正确信息,这次我们使用skipLines
值作为乘数。这样,即使您在名称列表中添加了更多详细信息,您也会发现仅修改skipLines
值的正确信息。
您需要订阅ListBox的SelectedIndexChanged事件:
Public Class Form1
Private people As String()
Private skipLines As Integer = 0
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
skipLines = 3
people = File.ReadAllLines([Source File])
For line As Integer = 0 To people.Length - 1
If line Mod skipLines = 0 Then
ListBox1.Items.Add(people(line))
End If
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
Dim StartIndex As Integer = ListBox1.SelectedIndex * skipLines
lblPhoneNumber.Text = people(StartIndex + 1)
lblAddress.Text = people(StartIndex + 2)
End Sub
End Class
这是如何工作的:
定义要跳过的行数。我们想要一行文字,那么跳过3行,这里:
Dim skipLines As Integer = 3
我们创建一个字符串数组。它将包含 File.ReadAllLines() 的输出,当然 returns 一个字符串数组:
Dim people As String() = File.ReadAllLines([Source File])
逐行迭代字符串数组的全部内容。一个集合枚举是从0
开始的,所以我们解析列表从0
到元素个数- 1
:
For line As Integer = 0 To people.Length - 1
'[...]
Next
当当前行号是skipLines
的倍数时满足If
条件。
Mod operator 将两个数相除,returns 运算的余数。如果没有提醒,line
数字是 skipLines
的倍数,我们要跳过的行数。
If line Mod skipLines = 0 Then
`[...]
End If
最后在满足条件的情况下,在当前[=49]代表的索引处添加字符串数组(people
数组的内容=]line
值,到ListBox.Items
集合:
ListBox1.Items.Add(people(line))