Listbox to textfile

Vyndrox

Member
Joined
Jan 6, 2004
Messages
13
Location
The Netherlands
***************a part of the source code*****************
Dim file As New System.IO.StreamWriter(filename)

For i = 0 To UBound(sFiles)
file.WriteLine(sFiles(i))
Next
file.Close()
ListBox1.Items.Clear()
ListBox1.Items.Add("The text file is written in the same directory as this executable, as " & filename)
btnSave.Enabled = False
btnShowFiles.Text = "Show Directory Files"
btnShowFiles.Focus()
ListBox1.Items.Add(Application.StartupPath & "\" & filename)
Catch ArgumentExep As System.ArgumentException
MsgBox(ArgumentExep.Message & vbLf & "Dont use illegal characters," & vbLf & "and wait 5 to 10 seconds to save to another file.")
Catch IOExep As System.IO.IOException
MsgBox(IOExep.Message & vbLf & "Wait 5 to 10 seconds and then save again, with another filename.")
End Try

I need the program to writes all the lines in the listbox to the .txt and not only the result. Maybe one of u guys have a solution for it.
 
what is the variable sFiles? Is that being written to a file for a reason?
If you want to save and load items from a ListBox why are you clearing it and adding non-listbox information to it?

You should be able to save the list with code similar to
Code:
Dim ListFile as new System.IO.StreamWriter("Random file name here.txt")

Dim i as string
for each i in listbox1.items
     listfile.writeline(i)
next
listfile.close()
 
Is the file open? Is it the same file you were using in Dim file As New System.IO.StreamWriter(filename)? If so you will need to do a file.Close().
 
Back
Top