saving an application

lukisf

Member
Joined
Apr 3, 2007
Messages
13
Programming Experience
1-3
Hi there,

I'm doing a project on vb.net 2005 and i want to create a save button in my application. the problem is the code to write on saving it. ex i have a combobox in which i'm adding different values in it. I need to save that in order to find all those i added remained there next time i run it.

any help is very much appreciated.

thank you very much

Regards,

Luke
 
VB.NET:
'Save the info
Dim sw As New System.IO.StreamWriter("YourFile")
For Each Item As String In ComboBox1.items
  sw.WriteLine(Item)
Next Item
sw.Close
 
'Read the info
Dim sr As New System.IO.StreamReader("YourFile")
Combobox1.items.Clear
While sr.Peek <> -1
  Combobox1.Items.Add(sr.ReadLine())
End While
sr.Close
 
Here's a little introducing serialization that can make these kind of save/load of file data easier. The items in Listbox is a ObjectCollection and isn't serializable, but this example copies the items to a string array. To better see the benefit of serialization here the items of two listboxes are saved:
VB.NET:
Sub savedata()
    Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
    Dim fs As New IO.FileStream("data.dat", IO.FileMode.Create, IO.FileAccess.Write)
    '
    Dim l(ListBox1.Items.Count - 1) As String
    ListBox1.Items.CopyTo(l, 0)
    bf.Serialize(fs, l)
    '
    ReDim l(ListBox2.Items.Count - 1)
    ListBox2.Items.CopyTo(l, 0)
    bf.Serialize(fs, l)
    '
    fs.Close()
End Sub
Deserializing back the items:
VB.NET:
Sub loaddata()
    Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
    Dim fs As New IO.FileStream("data.dat", IO.FileMode.Open, IO.FileAccess.Read)
    ListBox1.Items.AddRange(bf.Deserialize(fs))
    ListBox2.Items.AddRange(bf.Deserialize(fs))
    fs.Close()
End Sub
 
thanks

hi there thanks for the code. that really helped. Just another question please. when i'm dealing with adding records in a ms access database, i presume if i quit the application, the added records aren't saved in the db ej.
 
Is the Access database part of the project or are you remotely connecting? If it's part of the project then do a search on here for posts by Cjard, this problem is answered by him at least once weekly, he has a link in his signature describing the problem and how to fix it.
 
Back
Top