Support Forums

Full Version: Save Listbox
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a listbox as described in this thread... how do I save it so when I come back to the application next time it will load it?
For saving the listbox items and the passwords, the code is a bit more complicated...
I'm aware of that... do you know how?
OK here it is:
First, I made two subs, the LoadFromFile and SavetoFile customized to fit the needs of the app:
Code:
Public Sub SaveToFile(ByVal file As String)
        Dim text As String = "" 'this string represents what will be written to the file
        For Each item As String In ListBox1.Items
            text = text + item + "|" 'iterate through all items in the listbox and write them in text("|" will be used as delimeter when loading)
        Next
        text = text.Remove(text.Length - 1) 'last "|" is removed
        text = text + ";"

        For Each item As String In password
            text = text + item + "|" 'iterate through all items in the password list and write them in text("|" will be used as delimeter when loading)
        Next
        text = text.Remove(text.Length - 1) 'last "|" is removed

        System.IO.File.WriteAllText(file, text) 'write to file specified
    End Sub
    Public Sub LoadFromFile(ByVal file As String)
        If System.IO.File.Exists(file) Then 'tests if file specified exists
            Dim text As String = System.IO.File.ReadAllText(file) 'reads from file specified
            Dim lists() As String = text.Split(";") 'lists() will now contain 2 items: the 1st is listbox items, the 2nd password items
            Dim ListItems() As String = lists(0).Split("|") 'ListItems now contains listbox items
            Dim PasswordItems() As String = lists(1).Split("|") 'PasswordItems now contains password items

            'clear the listbox and password in case they have something
            ListBox1.Items.Clear()
            password.Clear()

            For Each item As String In ListItems
                ListBox1.Items.Add(item) 'iterate through all items in listitems and add them to the listbox
            Next

            For Each item As String In PasswordItems
                password.Add(item) 'iterate through all items in passworditems and add them to the password list
            Next
        End If
    End Sub
After you have added these subs to your code, then all you have to do is load the file when the app starts, and save to it when the app ends:
Code:
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        SaveToFile("example.txt")
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadFromFile("example.txt")
    End Sub

I hope this code helps and is easy to understand. I have added as many comments as I could.
Wow you rock! I am trying this right now.
(03-12-2010, 06:13 AM)thanasis2028 Wrote: [ -> ]OK here it is:
First, I made two subs, the LoadFromFile and SavetoFile customized to fit the needs of the app:
Code:
Public Sub SaveToFile(ByVal file As String)
        Dim text As String = "" 'this string represents what will be written to the file
        For Each item As String In ListBox1.Items
            text = text + item + "|" 'iterate through all items in the listbox and write them in text("|" will be used as delimeter when loading)
        Next
        text = text.Remove(text.Length - 1) 'last "|" is removed
        text = text + ";"

        For Each item As String In password
            text = text + item + "|" 'iterate through all items in the password list and write them in text("|" will be used as delimeter when loading)
        Next
        text = text.Remove(text.Length - 1) 'last "|" is removed

        System.IO.File.WriteAllText(file, text) 'write to file specified
    End Sub
    Public Sub LoadFromFile(ByVal file As String)
        If System.IO.File.Exists(file) Then 'tests if file specified exists
            Dim text As String = System.IO.File.ReadAllText(file) 'reads from file specified
            Dim lists() As String = text.Split(";") 'lists() will now contain 2 items: the 1st is listbox items, the 2nd password items
            Dim ListItems() As String = lists(0).Split("|") 'ListItems now contains listbox items
            Dim PasswordItems() As String = lists(1).Split("|") 'PasswordItems now contains password items

            'clear the listbox and password in case they have something
            ListBox1.Items.Clear()
            password.Clear()

            For Each item As String In ListItems
                ListBox1.Items.Add(item) 'iterate through all items in listitems and add them to the listbox
            Next

            For Each item As String In PasswordItems
                password.Add(item) 'iterate through all items in passworditems and add them to the password list
            Next
        End If
    End Sub
After you have added these subs to your code, then all you have to do is load the file when the app starts, and save to it when the app ends:
Code:
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        SaveToFile("example.txt")
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadFromFile("example.txt")
    End Sub

I hope this code helps and is easy to understand. I have added as many comments as I could.

Thanks.