Results 1 to 17 of 17

Thread: Reading from a textfile and adding the contents to different listboxes

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2009
    Posts
    82

    Reading from a textfile and adding the contents to different listboxes

    I have a form with 2 listboxes. I've added items in each listbox. I save all the items from each listbox to a text file. What I want to do is open the text file and I want each string inside the text file to go to the appropriate listbox. How can I make my program recognize where each string belongs?

  2. #2
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Reading from a textfile and adding the contents to different listboxes

    Use some sort of flag when saving, something that would never be in either listbox. For example:
    vb Code:
    1. 'Save routine
    2. Dim sw As New IO.StreamWriter(filename)
    3. Dim i As Integer = 0
    4. While i < Me.ListBox1.Items.Count
    5.      sw.WriteLine(CStr(Me.ListBox1.Items(i)))
    6.      i+=1
    7. End While
    8. i = 0
    9. sw.WriteLine("[SEPARATORSTRING]")
    10. While i < Me.ListBox2.Items.Count
    11.      sw.WriteLine(CStr(Me.ListBox2.Items(i)))
    12.      i+=1
    13. End While
    14. sw.Close()

    And do the opposite to save. Ignore that one line and switch listboxes after it.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reading from a textfile and adding the contents to different listboxes

    I would suggest using separate files.

    In fact, you don't even really need files at all. You could add a StringCollection to your application settings for each ListBox and then the data will be stored in the config file for you.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2009
    Posts
    82

    Re: Reading from a textfile and adding the contents to different listboxes

    Quote Originally Posted by minitech View Post
    Use some sort of flag when saving, something that would never be in either listbox. For example:
    vb Code:
    1. 'Save routine
    2. Dim sw As New IO.StreamWriter(filename)
    3. Dim i As Integer = 0
    4. While i < Me.ListBox1.Items.Count
    5.      sw.WriteLine(CStr(Me.ListBox1.Items(i)))
    6.      i+=1
    7. End While
    8. i = 0
    9. sw.WriteLine("[SEPARATORSTRING]")
    10. While i < Me.ListBox2.Items.Count
    11.      sw.WriteLine(CStr(Me.ListBox2.Items(i)))
    12.      i+=1
    13. End While
    14. sw.Close()

    And do the opposite to save. Ignore that one line and switch listboxes after it.
    I switched your code around a little bit but I worked off of it. Thank you. Check out the code, I have a little problem

    Save File code:
    VB .NET Code:
    1. Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
    2.         SaveFileDialog1.Filter = ".txt (*.txt)|*.txt"
    3.         SaveFileDialog1.DefaultExt = "txt"
    4.  
    5.         If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    6.  
    7.             Using myWriter As New System.IO.StreamWriter(SaveFileDialog1.FileName)
    8.  
    9.                 myWriter.WriteLine("|")
    10.  
    11.                 For Each Item As String In ListBox1.Items
    12.                     myWriter.WriteLine(Item)
    13.                 Next
    14.  
    15.                 myWriter.WriteLine("||")
    16.  
    17.                 For Each Item As String In ListBox2.Items
    18.                     myWriter.WriteLine(Item)
    19.                 Next
    20.  
    21.  
    22.             End Using
    23.         End If
    24.  
    25.     End Sub

    Open File code:
    VB .NET Code:
    1. Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
    2.         OpenFileDialog1.Filter = ".txt (*.txt)|*.txt"
    3.         OpenFileDialog1.DefaultExt = "txt"
    4.  
    5.         If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    6.  
    7.             Dim myReader As New System.IO.StreamReader(OpenFileDialog1.FileName)
    8.  
    9.             If myReader.ReadLine = "|" Then
    10.  
    11.                 Do Until myReader.ReadLine = "||"
    12.                     ListBox1.Items.Add(myReader.ReadLine)
    13.                 Loop
    14.  
    15.             End If
    16.  
    17.         End If
    18.  
    19.     End Sub

    As you can see, I've used the separator "|" for the listbox1 items and "||" for listbox2 items. In my open file coding, whenever I run the program, VB gives me an error on line 12 saying "Value cannot be null. Parameter name: item." What do I do about this? I can't find any way to properly work around it.
    Last edited by knicksfan426; May 24th, 2009 at 02:42 PM.

  5. #5
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Talking Re: Reading from a textfile and adding the contents to different listboxes

    One of the lines will be blank if you use .WriteLine(). Just add "On Error Resume Next" at the beginning of your Sub.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2009
    Posts
    82

    Re: Reading from a textfile and adding the contents to different listboxes

    Alright, I did what you said and the code now looks like this

    Open File code:
    vb Code:
    1. Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
    2.         On Error Resume Next
    3.  
    4.         OpenFileDialog1.Filter = ".txt (*.txt)|*.txt"
    5.         OpenFileDialog1.DefaultExt = "txt"
    6.  
    7.         If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    8.  
    9.             Dim myReader As New System.IO.StreamReader(OpenFileDialog1.FileName)
    10.  
    11.             If myReader.ReadLine = "|" Then
    12.  
    13.                 Do Until myReader.ReadLine = "||"
    14.                     ListBox1.Items.Add(myReader.ReadLine)
    15.                 Loop
    16.  
    17.             End If
    18.  
    19.         End If
    20.  
    21.     End Sub

    I still get an error in "Immediate Window" that says "A first chance exception of type 'System.ArgumentNullException' occurred in System.Windows.Forms.dll" What do I do?

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2009
    Posts
    82

    Re: Reading from a textfile and adding the contents to different listboxes

    bump

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reading from a textfile and adding the contents to different listboxes

    That's not an error, or at least not one you have to worry about. A message about a "first chance exception" means that an exception has been thrown, which is perfectly legal. It when an exception isn't caught that you have to worry. Does the application keep running and do what it's supposed to do?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Reading from a textfile and adding the contents to different listboxes

    First chance exceptions don't really seem to mean much. Just ignore them if you see that in the Debug window.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Feb 2009
    Posts
    82

    Re: Reading from a textfile and adding the contents to different listboxes

    Quote Originally Posted by jmcilhinney View Post
    That's not an error, or at least not one you have to worry about. A message about a "first chance exception" means that an exception has been thrown, which is perfectly legal. It when an exception isn't caught that you have to worry. Does the application keep running and do what it's supposed to do?
    No, the application freezes.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reading from a textfile and adding the contents to different listboxes

    I just noticed that you have On Error Resume Next in your code. Get rid of that and never use it again. Now your code will crash and we can see what the actual issue. Then we can fix it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Reading from a textfile and adding the contents to different listboxes

    1. Try taking out the OpenFileDialog1.DefaultExt line.
    2. Make sure the program has permissions to the file.
    3. Take out On Error Resume Next if it still doesn't work.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Feb 2009
    Posts
    82

    Re: Reading from a textfile and adding the contents to different listboxes

    Quote Originally Posted by minitech View Post
    1. Try taking out the OpenFileDialog1.DefaultExt line.
    2. Make sure the program has permissions to the file.
    3. Take out On Error Resume Next if it still doesn't work.
    I did everything you said, my application still freezes. To everyone, here's the open file dialog code:

    Note: Please remember the "|" is the separator I have made that comes before all listbox1 items and "||" is the separator that comes before all listbox2 items.

    .NET Code:
    1. Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
    2.         OpenFileDialog1.Filter = ".txt (*.txt)|*.txt"
    3.  
    4.         If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    5.  
    6.             Dim myReader As New System.IO.StreamReader(OpenFileDialog1.FileName)
    7.  
    8.             If myReader.ReadLine = "|" Then
    9.  
    10.                 Do Until myReader.ReadLine = "||"
    11.                     ListBox1.Items.Add(myReader.ReadLine)
    12.                 Loop
    13.  
    14.             End If
    15.  
    16.         End If
    17.  
    18.     End Sub
    19. End Class

    At the line of code where it says "ListBox1.Items.Add(myReader.ReadLine)", VB says there is an error there: "Value cannot be null.Parameter name: item" What do I do to fix this?
    Last edited by knicksfan426; May 29th, 2009 at 06:42 PM.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Feb 2009
    Posts
    82

    Re: Reading from a textfile and adding the contents to different listboxes

    bump

  15. #15
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Reading from a textfile and adding the contents to different listboxes

    Change Do Until myReader.ReadLine = "||" To Do Until (myReader.Peek() = -1) OrElse (myReader.ReadLine = "||").
    See if that fixes the problem.

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Feb 2009
    Posts
    82

    Re: Reading from a textfile and adding the contents to different listboxes

    Quote Originally Posted by minitech View Post
    Change Do Until myReader.ReadLine = "||" To Do Until (myReader.Peek() = -1) OrElse (myReader.ReadLine = "||").
    See if that fixes the problem.
    There is improvement but it is minimal. The file is opened with out any errors now but only part of the file is being opened, not all text is being distributed to the proper listboxes.

  17. #17
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Reading from a textfile and adding the contents to different listboxes

    That's because you haven't added a loop for the second listbox.
    After your first loop, add:
    vb.net Code:
    1. While myReader.Peek > -1
    2.      Me.ListBox2.Items.Add(myReader.ReadLine())
    3. End While

    By the way, if you haven't already, check your file; It needs to look like this:
    file Code:
    1. |
    2. List Box 1 Item 1
    3. List Box 1 Item 2
    4. ||
    5. List Box 2 Item 1
    6. List Box 2 Item 2
    7. List Box 3 Item 3

    If it doesn't, I have no idea what the problem is.
    Last edited by minitech; May 30th, 2009 at 07:51 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width