Saving a listbox

soadlink

Newcomer
Joined
Oct 12, 2003
Messages
15
Hello I am trying to save a listbox as a txt file.

Language: visual basic .net


i used the code below, and it actually saved the file, but the contents that where in the listbox were not saved with the file. if someone could modify my code below, or even better, make a small example :D, i would greatly appriciate it!

code i used:

SaveFileDialog1.Filter = "Text Documents (*.txt)|*.txt|All Files|*.*"
SaveFileDialog1.ShowDialog()
If SaveFileDialog1.FileName <> "" Then
FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)
PrintLine(1, listbox1.Text.Length)
FileClose(1)
End If


thank you
 
One way would be to do this:
Visual Basic:
Dim x As Integer
Dim writer As New IO.StreamWriter("E:\somefile.txt") 
'create a new streamwriter to write to the file
For x = 0 To l.Items.Count - 1 'make sure to go through every item
    writer.WriteLine(l.Items(x)) 'write the item to the file
Next
writer.Flush() 'make sure everything is dumped into the file
writer.Close() 'close the stream
 
cant seem to get it to work .. if possible could u attach the source to all the projects files? or give a different way?
 
it gave me a buncha errors, could you just make a little form with a save button, listbox, etc and upload it for me to dl? :( i just cant get it.
 
How would you expect us to help you if we don't know what's wrong?

You can copy the full text of an error by right clicking it in the Task List and choosing Copy.
 
i want them to be able to pick where it will be saved at.. so that code is wrong. please just make a form with a save button and listbox, and make it save all the contents of that listbox as a txt file.. please only reply now if you are going to attach the file
 
then dont respond, im sorry if im pushy its just so simple for you people but hard for me, so if you arent gonna post good help then dont post.
 
LOL, this post is making me laugh.

All you need to do is post your errors. I think it probley takes more effect to moan than do this simple task.

By the way there being pushy and just dam impolite, you figure.

Andy
 
Ok i got it to work, but i want it use the savefiledialog thingy so they can CHOOSE where the file will be saved.

how could that be pulled off?

thanks ;)
 
Simply put a SaveFileDialog onto your form, or create a new one in code. Then before saving anything show it using the ShowDialog method. After that, when createing a new StreamWriter pass in the SaveFileDialog1.FileName value which will contain the path which was selected.
 
:(

i have no clue how to do it, so assuming the save file dialogs name is savefiledialog1, how would the code look if the code i have now is the following:

Dim x As Integer
Dim writer As New IO.StreamWriter("c:\somefile.txt")
'create a new streamwriter to write to the file
For x = 0 To idlist.Items.Count - 1 'make sure to go through every item
writer.WriteLine(idlist.Items(x)) 'write the item to the file
Next
writer.Flush() 'make sure everything is dumped into the file
writer.Close() 'close the stream

thank you, i really appriciate the help :)
 
Visual Basic:
If SaveFileDialog1.ShowDialog = DialogResult.OK Then 'make sure a path was selected
   Dim x As Integer
   'subsitutue the path with the path from the dialog
   Dim writer As New IO.StreamWriter(SaveFileDialog1.FileName)
   'create a new streamwriter to write to the file
   For x = 0 To idlist.Items.Count - 1 'make sure to go through every item
   writer.WriteLine(idlist.Items(x)) 'write the item to the file
   Next
   writer.Flush() 'make sure everything is dumped into the file
   writer.Close() 'close the stream
End If
:)
 
edited post: woo hoo! works! and i got it to filter out so it only saves as a .txt.

thank you :)
 
Last edited:
Back
Top