Reading the lines of a text file and distributing the lines to separate listboxes

knicksfan426

New member
Joined
May 31, 2009
Messages
4
Programming Experience
Beginner
I have a form with 2 listboxes. I have 1 item added in each listbox. When I save the file, it contains the items within each listbox, I use a separator in the saved text file to denote which listbox each string is from.

This is my save file dialog code:

VB.NET:
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        SaveFileDialog1.Filter = ".txt (*.txt)|*.txt"

        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

            Using myWriter As New System.IO.StreamWriter(SaveFileDialog1.FileName)

                myWriter.WriteLine("|")

                For Each Item As String In ListBox1.Items
                    myWriter.WriteLine(Item)
                Next

                myWriter.WriteLine("||")

                For Each Item As String In ListBox2.Items
                    myWriter.WriteLine(Item)
                Next

            End Using
        End If

    End Sub

As you can see, | is the separator that comes before listbox1 items and || is the separator that comes before listbox2 items in the saved text file. Now, what I want to do is open this file and distribute each string in the file to the appropriate listbox.

This is what the saved text file looks like:

PHP:
|
asd
bsd
||

This is my code for the open file dialog:

VB.NET:
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        OpenFileDialog1.Filter = ".txt (*.txt)|*.txt"

        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            
            Using myReader As New System.IO.StreamReader(OpenFileDialog1.FileName)

                If myReader.ReadLine = "|" Then
                    Dim fileStrings As String = myReader.ReadLine()

                    Do Until myReader.ReadLine = "||"
                        ListBox1.Items.Add(fileStrings)
                    Loop
                End If

            End Using

        End If

    End Sub

As you see, if the streamreader reads the line "|" (the listbox1 separator), then I have instructed the program to keep adding whatever the streamreader reads into listbox1 UNTIL the streamreader reads the line "||" (the listbox2 separator).

Now what happens when I open the file is that only the "asd" line is added into listbox1. The reader for some reason wont keep looping to the next line until it reads the listbox2 separator (||). I need it to add EVERYTHING into listbox1 until it reads ||. What do I do to fix this? Sorry, I know it's a lot of writing.
 
You have to declare a variable to keep the String value returned from each ReadLine call, then analyze that variable value and do what is appropriate with it.
 
I haven't had time to test this but it looks like it will work.

VB.NET:
Using myReader As New System.IO.StreamReader(OpenFileDialog.Filename)

    'read first line
    Dim fileStrings As String = myReader.ReadLine()

    'check for listbox indicator
    If fileStrings = "|" Then

        'read next line
        fileStrings = myReader.ReadLine()

        'begin loop
        Do Until fileStrings = "||"

            'add to listbox
            ListBox1.Items.Add(fileStrings)

            'read next line
            fileStrings = myReader.ReadLine()

        Loop

    End If

End Using
 
I haven't had time to test this but it looks like it will work.

VB.NET:
Using myReader As New System.IO.StreamReader(OpenFileDialog.Filename)

    'read first line
    Dim fileStrings As String = myReader.ReadLine()

    'check for listbox indicator
    If fileStrings = "|" Then

        'read next line
        fileStrings = myReader.ReadLine()

        'begin loop
        Do Until fileStrings = "||"

            'add to listbox
            ListBox1.Items.Add(fileStrings)

            'read next line
            fileStrings = myReader.ReadLine()

        Loop

    End If

End Using
Thank you! It works! I'm gonna examine it and fully understand it later as im loaded with work right now. I might ask some questions later. Thanks so much.
 
Back
Top