Braden Putt
Braden Putt

Reputation: 61

Read every fourth line from a text file

So, I have a text file with the following:

Andrew Law
0276376352
13 Parsons St
Kevin Kyle
0376458374
29 Penrod Drive
Billy Madison
06756355
16 Stafford Street

Now on my Form, I have a ListBox. When the Form loads, I would like to read every fourth line from the text file (every name) and display it in the ListBox.

All I have right now is the following:

Dim People As String
People = System.IO.File.ReadAllLines("filelocation")(3)
ListBox1.Items.Add(People)

This however only reads line number 4 where as I want to read EVERY fourth line after that also.

Upvotes: 0

Views: 169

Answers (1)

Jimi
Jimi

Reputation: 32278

Add all the strings extracted from a source file, when the current line is a multiple of a pre-defined number of lines to skip or 0, to a ListBox and allow a user to select a Name from the list to fill some Labels with the details related to the selected Name.

  • Parse and extract the People Names from the array of string. Each Name can be found at an index which is multiple of the value specified by the skipLines field.
  • Add each Name to a ListBox control.
  • When a Name is selected from the ListBox list of names, add the related details to some Labels (named lblPhoneNumber and lblAddress here). To identify the correct informations in the array, we're using the skipLines value as a multiplier this time. This way, even if you add some more details in the list of names, you'll find the right informations modifying just the skipLines value.
    You need to subscribe to the SelectedIndexChanged event of the ListBox:

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

How this works:

Define the number of lines to skip. We want a line of text, then skip 3 lines, here:

Dim skipLines As Integer = 3

We create an array of strings. It will contain the output of File.ReadAllLines(), which of course returns an array of strings:

Dim people As String() = File.ReadAllLines([Source File])

Iterate the whole content of the array of strings, line by line. A collection enumeration starts from 0, so we parse the list from 0 to the number of elements - 1:

For line As Integer = 0 To people.Length - 1
'[...]
Next

The If condition if met when the current line number is a multiple of skipLines.
The Mod operator divides two numbers and returns the remainder of the operation. If there's no reminder, the line number is a multiple of skipLines, the number of lines we want to skip.

If line Mod skipLines = 0 Then
`[...]
End If

Finally, when the condition is met, add the content of the array of strings (the people array) at the index represented by the current line value, to the ListBox.Items collection:

ListBox1.Items.Add(people(line))

Upvotes: 2

Related Questions