[Tutorial]Saving/Loading a Listbox Contents

Posts 1–14 of 14 · Page 1 of 1
[Tutorial]Saving/Loading a Listbox Contents
Ok guys so heres a short simple tutorial on how to Save/Load a listbox and its contents.
1. Add a listbox to a form(obviously)
2. Now under public class form 1 add this:
Code:
Inherits System.Windows.Forms.Form
Dim w As IO.StreamWriter
    Dim r As IO.StreamReader
3. Now to save a listbox contents use this code:
Code:
Dim i As Integer
        w = New IO.StreamWriter(Save Location Here. Save as .txt file)
        For i = 0 To ListBox1.Items.Count - 1
            w.WriteLine(ListBox1.Items.Item(i))
        Next
        w.Close()
4. Now to load a listbox contents.
Code:
r = New IO.StreamReader("Load location here. must load from .txt file)
        While (r.Peek() > -1)
            ListBox1.Items.Add(r.ReadLine)
        End While
        r.Close()
Its as simple as that.
Now for a side note: You must save a listbox contents to a .txt file before you can load them. Also the save location should be as same as the save location.
Now the entire code put together:
Code:
Public Class Form1
    Inherits System.Windows.Forms.Form
 Dim w As IO.StreamWriter
    Dim r As IO.StreamReader

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        w = New IO.StreamWriter("c:\test.txt")
        For i = 0 To ListBox1.Items.Count - 1
            w.WriteLine(ListBox1.Items.Item(i))
        Next
        w.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        r = New IO.StreamReader("c:\test.txt")
        While (r.Peek() > -1)
            ListBox1.Items.Add(r.ReadLine)
        End While
        r.Close()
    End Sub
End Class
Looks Good , Thank you
Quote Originally Posted by NextGen1 View Post
Looks Good , Thank you
Thanks and i can say confidently that this tut isnt leeched since i wrote it up myself xD
Sorry if i bumped this but i hav a question
can u do this same with a txt box
Quote Originally Posted by hopefordope View Post
Sorry if i bumped this but i hav a question
can u do this same with a txt box
Yes, although the saving/loading code wouldnt be the same.
you cant use items.add but yes, you can
looks good.
so would this work for .xml files as well?
Quote Originally Posted by XGelite View Post
so would this work for .xml files as well?
Although it would not be the same save/load code i suppose it could be implemented for .xml files
Quote Originally Posted by Zoom View Post
Great tutorial!
Thank you
Great tutorial!
so what would it be if i wanted to save it to the desktop
w = New IO.StreamWriter(Save Location Here. Save as .txt file)
"C:\Documents and Settings\User\USERNAME
\Desktop\"

so.....

Code:
w = New IO.StreamWriter("C:\Documents and Settings\User\USERNAME
\Desktop\sample.txt")
You can use the code

Code:
Enviroment.Username
to get the username

so to save to anyones desktop

Code:
dim usern as enviroment.username

w = New IO.StreamWriter("C:\Documents and Settings\User\" & usern & "\Desktop\sample.txt")
Quote Originally Posted by NextGen1 View Post
"C:\Documents and Settings\User\USERNAME
\Desktop\"

so.....

Code:
w = New IO.StreamWriter("C:\Documents and Settings\User\USERNAME
\Desktop\sample.txt")
You can use the code

Code:
Enviroment.Username
to get the username

so to save to anyones desktop

Code:
dim usern as enviroment.username

w = New IO.StreamWriter("C:\Documents and Settings\User\" & usern & "\Desktop\sample.txt")
it says that Type'Environment.username' is not defined..... i tryed the usern = blalalal method but it still didnt work this is my [php]Dim i As Integer
Dim usern As Environment.Username
w = New IO.StreamWriter("C:\Documents and Settings\User\" & usern & "\Desktop\sample.txt")
For i = 0 To Dlls.Items.Count - 1
w.WriteLine(Dlls.Items.Item(i))
Next
w.Close()[/php]
Hehehe good job, great tut
Quote Originally Posted by Pixipixel_ View Post
Hehehe good job, great tut
Thanks man
Posts 1–14 of 14 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us