Settings

Do you need something made? then ask in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
11 posts Page 1 of 2
Contributors
User avatar
NeedHelp
Member
Member
Posts: 42
Joined: Fri Feb 19, 2010 3:57 pm

Settings
NeedHelp
How can i make settings for a listbox, like this:

All the items in listbox1 will be saved everytime the program is closed and it will be there when you open it.
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

Re: Settings
CodenStuff
Hello,

It would be best to use a text file for this:

To Save:
Code: Select all
 With ListBox1
            Dim t As Integer
            Dim s As String = ""
            For t = 0 To .Items.Count - 1
                s += .Items(t).ToString & vbNewLine
            Next
            Try
                My.Computer.FileSystem.DeleteFile("C:\MyListbox.txt", FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)
            Catch ex As Exception
            End Try
            My.Computer.FileSystem.WriteAllText("C:\MyListbox.txt", s, False)
        End With
To Load:
Code: Select all
Using sr As New IO.StreamReader("C:\MyListbox.txt")
            Dim item As String = sr.ReadLine
            While item <> Nothing
                ListBox1.Items.Add(item)
                item = sr.ReadLine
            End While
        End Using
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
NeedHelp
Member
Member
Posts: 42
Joined: Fri Feb 19, 2010 3:57 pm

Re: Settings
NeedHelp
Where can i add those?
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

Re: Settings
CodenStuff
Hello,

You could put the save code into the "Form_Closing" event and the load code into the "Form_Load" event handlers.
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
NeedHelp
Member
Member
Posts: 42
Joined: Fri Feb 19, 2010 3:57 pm

Re: Settings
NeedHelp
Image
Lewis
Coding God
Coding God
Posts: 1564
Joined: Sun Dec 20, 2009 2:12 pm

Re: Settings
Lewis
Ahhh... CodenStuff has forgot too tell you that you will need to manualy create C:\MyListbox.txt

But may i recomend putting the MyListbox.txt in the storage folder.. ?
Image
User avatar
NeedHelp
Member
Member
Posts: 42
Joined: Fri Feb 19, 2010 3:57 pm

Re: Settings
NeedHelp
Like in resource?
Lewis
Coding God
Coding God
Posts: 1564
Joined: Sun Dec 20, 2009 2:12 pm

Re: Settings
Lewis
No in the folder were the EXE is :)
Application.StartupPath + "/MyListbox.txt"
Image
User avatar
NeedHelp
Member
Member
Posts: 42
Joined: Fri Feb 19, 2010 3:57 pm

Re: Settings
NeedHelp
HOW!?
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

Re: Settings
CodenStuff
Hello,

You need to check if the file exists first like:

For the load code:
Code: Select all
If Not My.Computer.Filesystem.FileExists("C:\MyListbox.txt") Then
    My.Computer.FileSystem.WriteAllText("C:\MyListbox.txt", "", False)
End If
Using sr As New IO.StreamReader("C:\MyListbox.txt")
            Dim item As String = sr.ReadLine
            While item <> Nothing
                ListBox1.Items.Add(item)
                item = sr.ReadLine
            End While
        End Using
If you want to have the file in your application directory you need to change "C:\MyListbox.txt" in both the save and load code to this:
Code: Select all
Application.StartupPath & "\MyListbox.txt"
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
11 posts Page 1 of 2
Return to “Tutorial Requests”