Results 1 to 10 of 10

Thread: Saving and loading a listbox from .txt

  1. #1

    Thread Starter
    Addicted Member VBGangsta's Avatar
    Join Date
    Aug 2003
    Location
    New York
    Posts
    219

    Saving and loading a listbox from .txt

    I cant seem to fing the this answer in .net so...I need to save listbox1 to a .txt in my documeants and then be able to load it. How whould i do this? Thanks
    -Rob

  2. #2
    Junior Member
    Join Date
    Dec 2003
    Location
    California
    Posts
    19
    You need to use the System.IO.FileStream, System.IO.StreamWriter, and System.IO.StreamReader classes..

    Ex:
    VB Code:
    1. Private Sub readComboItems(.......
    2.      Dim fs as System.IO.FileStream
    3.      Dim sw as System.IO.StreamWriter
    4.      Dim its as String
    5.            fs = New System.IO.FileStream("your file path\filename.txt", IO.FileMode.OpenOrCreate)
    6.            sw = System.IO.StreamWriter(fs)
    7.  
    8.            For each its in Me.Combo1.Items
    9.               sw.WriteLine
    10.               sw.Flush
    11.            Next
    12.  
    13. End Sub

    To read them back into a combo box:
    VB Code:
    1. Private Sub writeComboItems(.......
    2.      Dim fs as System.IO.FileStream
    3.      Dim sr as System.IO.StreamReader
    4.      Dim lineVal as String
    5.            fs = New System.IO.FileStream("your file path\filename.txt", IO.FileMode.Open)
    6.            sr = System.IO.StreamReader(fs)
    7.  
    8.            Do
    9.                lineVal = sr.ReadLine
    10.                If lineVal is nothing then exit do
    11.                me.ComboBox1.Items.Add(lineVal)
    12.           Loop
    13. End Sub

    Hope this helps
    Regards-

  3. #3
    Junior Member
    Join Date
    Dec 2003
    Location
    California
    Posts
    19
    Sorry. Just noticed you said listbox not combo box...too much NyQuil... Should work the same regardless.

  4. #4

    Thread Starter
    Addicted Member VBGangsta's Avatar
    Join Date
    Aug 2003
    Location
    New York
    Posts
    219
    thanks for the response. It says sw = System.IO.StreamWriter(fs) is an IO and can not be used as an expression
    -Rob

  5. #5
    Junior Member
    Join Date
    Dec 2003
    Location
    California
    Posts
    19
    Send me what you have written. Take out any proprietary info first.

  6. #6

    Thread Starter
    Addicted Member VBGangsta's Avatar
    Join Date
    Aug 2003
    Location
    New York
    Posts
    219
    hers what i got
    VB Code:
    1. Dim fs As System.IO.FileStream
    2.         Dim sw As System.IO.StreamWriter
    3.         Dim its As String
    4.         fs = New System.IO.FileStream("C:\My documents\attacklist.txt", IO.FileMode.OpenOrCreate)
    5.         sw = System.IO.StreamWriter(fs)
    6.  
    7.         For Each its In Me.ListBox1.Items
    8.             sw.WriteLine()
    9.             sw.Flush()
    10.         Next
    -Rob

  7. #7

    Thread Starter
    Addicted Member VBGangsta's Avatar
    Join Date
    Aug 2003
    Location
    New York
    Posts
    219
    nvm i got it to work, i changed it around a little:

    VB Code:
    1. Dim SW As StreamWriter
    2.             Dim Fs As FileStream
    3.         Fs = New FileStream("C:\my documents\attacklist.txt", FileMode.Create)
    4.             SW = New StreamWriter(Fs)
    5.             Dim itm As Object
    6.             For Each itm In ListBox1.Items
    7.                 SW.WriteLine(itm.ToString)
    8.             Next
    9.             SW.Close()
    10.             Fs.Close()
    -Rob

  8. #8

    Thread Starter
    Addicted Member VBGangsta's Avatar
    Join Date
    Aug 2003
    Location
    New York
    Posts
    219
    now useing that code how whould i use the save dialog function?
    -Rob

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by VBGangsta
    now useing that code how whould i use the save dialog function?
    try this now :
    VB Code:
    1. Dim SW As StreamWriter
    2.         Dim Fs As FileStream
    3.  
    4.         Dim fd As New SaveFileDialog
    5.         If fd.ShowDialog = DialogResult.OK Then
    6.             Fs = New FileStream(fd.FileName, FileMode.Create)
    7.             SW = New StreamWriter(Fs)
    8.             Dim itm As Object
    9.             For Each itm In ListBox1.Items
    10.                 SW.WriteLine(itm.ToString)
    11.             Next
    12.             SW.Close()
    13.             Fs.Close()
    14.         End If

  10. #10
    Junior Member
    Join Date
    Dec 2003
    Location
    California
    Posts
    19
    Thanks Pirate.

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