Save Checks in a checkedlistbox

Lanc1988

Contributor
Joined
Nov 27, 2003
Messages
508
I have a checkedlistbox control on my form and it saves the items in the list with the code below, but it does not save if they have been checked.. so I would like it to remember if they were checked, that way when it loads the items back into the listbox it will have them checked.

Save code:
Visual Basic:
        'Saves Misc Goals
        Dim Miscfs As New System.IO.FileStream(Application.StartupPath & "\MiscGoals.dat", System.IO.FileMode.Create)
        Dim Miscsw As New System.IO.StreamWriter(Miscfs)
        Dim obj As Object
        For Each obj In listboxMiscGoals.Items
            Miscsw.WriteLine(obj.ToString())
        Next obj
        Miscsw.Close()

Load code:
Visual Basic:
        'Loads Misc Goals into listbox
        Dim Miscfs As New System.IO.FileStream(Application.StartupPath & "\MiscGoals.dat", System.IO.FileMode.Open)
        Dim Miscsr As New System.IO.StreamReader(Miscfs)
        Dim Miscline As String
        Do
            Miscline = Miscsr.ReadLine
            If Miscline Is Nothing Then
                Exit Do
            Else
                listboxMiscGoals.Items.Add(Miscline)
            End If
        Loop
        Miscsr.Close()
 
Visual Basic:
        'Saves Misc Goals
        Dim Miscfs As New System.IO.FileStream(Application.StartupPath & "/MiscGoals.dat", System.IO.FileMode.Create)
        Dim Miscsw As New System.IO.StreamWriter(Miscfs)
       
        Dim i As Integer
        For i = 0 To listboxMiscGoals.CheckedItems.Count - 1
            Miscsw.WriteLine(listboxMiscGoals.CheckedItems.Item(i).ToString)
        Next
        Miscsw.Close()



Visual Basic:
        'Loads Misc Goals into listbox
        Dim Miscfs As New System.IO.FileStream(Application.StartupPath & "/MiscGoals.dat", System.IO.FileMode.Open)
        Dim Miscsr As New System.IO.StreamReader(Miscfs)
        Dim Miscline As String
        Dim i As Integer
        Do
            Miscline = Miscsr.ReadLine
            If Miscline Is Nothing Then
                Exit Do
            Else
                For i = 0 To listboxMiscGoals.Items.Count - 1
                    If listboxMiscGoals.Items.Item(i).ToString = Miscline Then
                        listboxMiscGoals.SetItemChecked(i, True)
                    End If
                Next

            End If
        Loop
        Miscsr.Close()



More info on the checkListBox in the VB@TheMovies.

You can download Here the movie on the Check List Box
 
won't that save code you posted 'only' say the checked items, or will it still save them all but will remember if certain ones were checked?

I need the code that will save every item in the list and if any are checked, it will remember when it loads them back into the listbox next time.
 
yeah, i just tested the code you posted and it only saves checked ones, i want it to save them all, but when it loads, check the ones that were checked.
 
well if the checkbox has always the same items, why the need for that?
And anyway, try to figure it out by yourself, it's a good programming lesson.

save them all in the file and for the one checked put a special character before, or just list them after etc
 
I was thinking I might try saying either a 1 or 0 after the item, 1 being if it is checked and 0 if its not, then when i load, if it has a 1 it will check it
 
Back
Top