Save Command

WackoWolf
09-11-2004, 06:00 PM
I am trying to finish a project for school, but I seem to have bitten off more then I could chew. I decided to add a Save Button, but I am lost as how to do this. I need to save what is added to a ListBox. Could somone Please help.

Thank You
JLGelsomino@cox.net

shaul_ahuva
09-11-2004, 06:47 PM
I am trying to finish a project for school, but I seem to have bitten off more then I could chew. I decided to add a Save Button, but I am lost as how to do this. I need to save what is added to a ListBox. Could somone Please help.

Thank You
JLGelsomino@cox.net

One way you could do it is:

Imports System.IO

Dim sw As New StreamWriter("c:\listboxcontents.txt")

For i As Integer = 0 To ListBox1.Items.Count - 1
sw.WriteLine(ListBox1.Items(i).ToString())
Next i

sw.Close()


This will create the text file or open it. If it already exists and you just want to append, use one of the overloaded constructors. Otherwise, it will overwrite your file.

MKoslof
09-11-2004, 07:27 PM
What are you saving to? Do you want to send this to a text file? Are you saving this information to a DataSet, a database table? etc.

WackoWolf
09-12-2004, 07:37 AM
I want to save to a Text file.

MKoslof
09-12-2004, 10:34 AM
Then the example in post2 should work OK :). The only thing I would add is, after your loop and before the sw.close event, put in the .flush method so you accurately dump all the info into the newly created text file.



'after your write line loop
sw.Flush
sw.close



And you might want to check if the value in the Listbox's current list index is NOT nothing or greater than "" before writing it to the text file.

WackoWolf
09-12-2004, 03:07 PM
This is what I have for the code to save as a text file. But it has an error saying that StreamWriter is not defined
How do I fix this?




(code)
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim sw As New StreamWriter("c:\listboxcontents.txt")

For i As Integer = 0 To lstNames.Items.Count - 1
sw.WriteLine(lstNames.Items(i).ToString())
Next i
sw.Flush()
sw.Close()
End Sub
End Class(code)

WackoWolf
09-12-2004, 03:15 PM
This is what I change the code to, and now it works.
Thank You for all the Help you gave me. You people are the best.

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

Thanks Again

WackoWolf
09-12-2004, 03:16 PM
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim SW As System.IO.StreamWriter = New System.IO.StreamWriter("C:\lstNames.txt")
For i As Integer = 0 To lstNames.Items.Count - 1
sw.WriteLine(lstNames.Items(i).ToString())
Next i
sw.Flush()
sw.Close()

MKoslof
09-12-2004, 06:25 PM
Looks good..glad we could be of assistance. Good luck.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum