Ok guys so heres a short simple tutorial on how to Save/Load a listbox and its contents.
1. Add a listbox to a form(obviously)
2. Now under public class form 1 add this:
Code:
Inherits System.Windows.Forms.Form
Dim w As IO.StreamWriter
Dim r As IO.StreamReader
3. Now to save a listbox contents use this code:
Code:
Dim i As Integer
w = New IO.StreamWriter(Save Location Here. Save as .txt file)
For i = 0 To ListBox1.Items.Count - 1
w.WriteLine(ListBox1.Items.Item(i))
Next
w.Close()
4. Now to load a listbox contents.
Code:
r = New IO.StreamReader("Load location here. must load from .txt file)
While (r.Peek() > -1)
ListBox1.Items.Add(r.ReadLine)
End While
r.Close()
Its as simple as that.
Now for a side note: You must save a listbox contents to a .txt file before you can load them. Also the save location should be as same as the save location.
Now the entire code put together:
Code:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim w As IO.StreamWriter
Dim r As IO.StreamReader
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
w = New IO.StreamWriter("c:\test.txt")
For i = 0 To ListBox1.Items.Count - 1
w.WriteLine(ListBox1.Items.Item(i))
Next
w.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
r = New IO.StreamReader("c:\test.txt")
While (r.Peek() > -1)
ListBox1.Items.Add(r.ReadLine)
End While
r.Close()
End Sub
End Class