Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Save Listbox
#1
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?
Reply
#2
For saving the listbox items and the passwords, the code is a bit more complicated...
My software company: Porosis Software
Games: Terrantula
Apps: Mathanasis, ColorGrabber
Reply
#3
I'm aware of that... do you know how?
Reply
#4
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.
My software company: Porosis Software
Games: Terrantula
Apps: Mathanasis, ColorGrabber
Reply
#5
Wow you rock! I am trying this right now.
Reply
#6
(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.
[Image: 20r9vh4.jpg]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TextBox2.Text | Transfer Text to ListBox Die 3 2,673 01-02-2012, 06:09 PM
Last Post: AceInfinity
  [VB.NET] - ListBox Drag & Drop - [TUT] Fragma 10 7,732 08-25-2011, 07:02 PM
Last Post: AceInfinity
  Need some help - ListBox Counter Fragma 14 4,922 07-03-2011, 05:33 AM
Last Post: H-Q
  [Visual Basic] Midnight Club : La Save editor [Source!] Coding Support 6 3,480 06-22-2011, 12:09 PM
Last Post: Coding Support
  [HELP] Open and save txt file to the internet. w00pz 5 1,519 05-29-2011, 01:34 PM
Last Post: w00pz

Forum Jump:


Users browsing this thread: 1 Guest(s)