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
Printable View
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
You need to use the System.IO.FileStream, System.IO.StreamWriter, and System.IO.StreamReader classes..
Ex:
VB Code:
Private Sub readComboItems(....... Dim fs as System.IO.FileStream Dim sw as System.IO.StreamWriter Dim its as String fs = New System.IO.FileStream("your file path\filename.txt", IO.FileMode.OpenOrCreate) sw = System.IO.StreamWriter(fs) For each its in Me.Combo1.Items sw.WriteLine sw.Flush Next End Sub
To read them back into a combo box:
VB Code:
Private Sub writeComboItems(....... Dim fs as System.IO.FileStream Dim sr as System.IO.StreamReader Dim lineVal as String fs = New System.IO.FileStream("your file path\filename.txt", IO.FileMode.Open) sr = System.IO.StreamReader(fs) Do lineVal = sr.ReadLine If lineVal is nothing then exit do me.ComboBox1.Items.Add(lineVal) Loop End Sub
Hope this helps
Regards-
Sorry. Just noticed you said listbox not combo box...too much NyQuil... Should work the same regardless.
thanks for the response. It says sw = System.IO.StreamWriter(fs) is an IO and can not be used as an expression
Send me what you have written. Take out any proprietary info first.
hers what i got
VB Code:
Dim fs As System.IO.FileStream Dim sw As System.IO.StreamWriter Dim its As String fs = New System.IO.FileStream("C:\My documents\attacklist.txt", IO.FileMode.OpenOrCreate) sw = System.IO.StreamWriter(fs) For Each its In Me.ListBox1.Items sw.WriteLine() sw.Flush() Next
nvm i got it to work, i changed it around a little:
VB Code:
Dim SW As StreamWriter Dim Fs As FileStream Fs = New FileStream("C:\my documents\attacklist.txt", FileMode.Create) SW = New StreamWriter(Fs) Dim itm As Object For Each itm In ListBox1.Items SW.WriteLine(itm.ToString) Next SW.Close() Fs.Close()
now useing that code how whould i use the save dialog function?
try this now :Quote:
Originally posted by VBGangsta
now useing that code how whould i use the save dialog function?
VB Code:
Dim SW As StreamWriter Dim Fs As FileStream Dim fd As New SaveFileDialog If fd.ShowDialog = DialogResult.OK Then Fs = New FileStream(fd.FileName, FileMode.Create) SW = New StreamWriter(Fs) Dim itm As Object For Each itm In ListBox1.Items SW.WriteLine(itm.ToString) Next SW.Close() Fs.Close() End If
Thanks Pirate.:D