Results 1 to 17 of 17

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

Hybrid View

  1. #1
    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.

  2. #2

    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.

  3. #3
    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.

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