|
-
Dec 8th, 2003, 09:44 PM
#1
Thread Starter
Addicted Member
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
-
Dec 8th, 2003, 11:08 PM
#2
Junior Member
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-
-
Dec 8th, 2003, 11:10 PM
#3
Junior Member
Sorry. Just noticed you said listbox not combo box...too much NyQuil... Should work the same regardless.
-
Dec 8th, 2003, 11:34 PM
#4
Thread Starter
Addicted Member
thanks for the response. It says sw = System.IO.StreamWriter(fs) is an IO and can not be used as an expression
-
Dec 8th, 2003, 11:43 PM
#5
Junior Member
Send me what you have written. Take out any proprietary info first.
-
Dec 9th, 2003, 12:09 AM
#6
Thread Starter
Addicted Member
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
-
Dec 9th, 2003, 12:31 AM
#7
Thread Starter
Addicted Member
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()
-
Dec 9th, 2003, 12:39 AM
#8
Thread Starter
Addicted Member
now useing that code how whould i use the save dialog function?
-
Dec 9th, 2003, 01:00 PM
#9
Sleep mode
Originally posted by VBGangsta
now useing that code how whould i use the save dialog function?
try this now :
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
-
Dec 9th, 2003, 02:05 PM
#10
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|