How to store contents of a CheckedListBox in My.Settings

Ron Sipherd 241 Reputation points
2021-09-09T03:05:46.79+00:00

I have a CheckedListBox object on a VB 2019 form; each line consists as you might expect, of a checkbox and a string.
I want to save the contents of the lines to My.Settings so they will persist between uses.
The strings are no problem, I presently save them all as a single string with a question-mark separator and use String.Split to build an array of the individual values.
But I don't see an obvious way to store the checkbox settings and tie each to its associated string.
They can be Boolean values, so I can store them as 11000101 etc. matching the order of the strings in the box.
Or I could start each string in the array with a 1 or a 0.
Beyond that, things like custom Objects and Settings Providers seem like overkill.
Am I missing something simple here?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,668 questions
{count} votes

5 answers

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,316 Reputation points
    2021-09-09T05:19:01.24+00:00

    Hi Ron,
    you can add in Settings a place holder for string:

    130653-x.png

    And in code behind in Form.Load extract "ClbList" to list of Data, insert Items and in Form.Closing build new list of Data, serialize to XML and save this list in Settings. Try following demo (insert code in empty form):

    Imports System.IO  
    Imports System.Xml.Serialization  
      
    Public Class Form1  
      
      Private Clb As New CheckedListBox With {.Dock = DockStyle.Fill}  
      
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        Me.Controls.Add(Clb)  
      
        Dim s As String = My.Settings.ClbList  
        Dim l As List(Of Data)  
      
        ' for first start generate demo data '  
        If String.IsNullOrEmpty(s) Then  
          Dim rnd As New Random  
          l = New List(Of Data)  
          For i = 1 To 10  
            l.Add(New Data With {.CB = rnd.NextDouble < 0.5, .TXT = $"Item {i}"})  
          Next  
        Else  
          ' load data from struing in Settings '  
          l = CType(New XmlSerializer(GetType(List(Of Data))).Deserialize(New StringReader(s)), List(Of Data))  
        End If  
      
        For Each item In l  
          Dim i = Me.Clb.Items.Add(item)  
          Me.Clb.SetItemCheckState(i, If(item.CB, CheckState.Checked, CheckState.Unchecked))  
        Next  
      End Sub  
      
      Public Class Data  
        Public Property CB As Boolean  
        Public Property TXT As String  
      
        Public Overrides Function ToString() As String  
          Return Me.TXT  
        End Function  
      End Class  
      
      Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing  
        Dim l As New List(Of Data)  
        For i = 0 To Me.Clb.Items.Count - 1  
          Dim item As Data = CType(Me.Clb.Items(i), Data)  
          item.CB = Me.Clb.GetItemCheckState(i) = CheckState.Checked  
          l.Add(item)  
        Next  
        Dim wrt As New StringWriter()  
        Call New XmlSerializer(GetType(List(Of Data))).Serialize(wrt, l)  
        wrt.Close()  
        My.Settings.ClbList = wrt.ToString  
        My.Settings.Save()  
      End Sub  
      
    End Class  
    
    0 comments No comments

  2. Karen Payne MVP 35,386 Reputation points
    2021-09-09T13:21:42.813+00:00

    Rather than storing data in MySettings, consider storing data in a Json file.

    Form code (there are classes and modules also)

    Public Class JsonForm  
        Private Sub JsonForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            MonthCheckedListBox.LoadJson()  
        End Sub  
        Private Sub JsonForm_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing  
            MonthCheckedListBox.SaveToJson()  
        End Sub  
    End Class  
    

    130724-checkedlistboxjson.png

    For sample data I used month names

    [  
      {  
        "Text": "January",  
        "Checked": false  
      },  
      {  
        "Text": "February",  
        "Checked": true  
      },  
      {  
        "Text": "March",  
        "Checked": false  
      },  
      {  
        "Text": "April",  
        "Checked": false  
      },  
      {  
        "Text": "May",  
        "Checked": true  
      },  
      {  
        "Text": "June",  
        "Checked": false  
      },  
      {  
        "Text": "July",  
        "Checked": false  
      },  
      {  
        "Text": "August",  
        "Checked": false  
      },  
      {  
        "Text": "September",  
        "Checked": false  
      },  
      {  
        "Text": "October",  
        "Checked": false  
      },  
      {  
        "Text": "November",  
        "Checked": false  
      },  
      {  
        "Text": "December",  
        "Checked": true  
      }  
    ]  
    

    Project overview

    130717-code-1.png

    0 comments No comments

  3. Elizabeth Bradshaw 1 Reputation point
    2021-09-09T13:26:37.727+00:00

    i have a boyfriend i dont need a house yet i am sorry


  4. Ron Sipherd 241 Reputation points
    2021-09-09T16:28:01.85+00:00

    Thanks! Will take a closer look at these ideas.
    (Except for that last comment, which I don't understand either.)

    0 comments No comments

  5. Ron Sipherd 241 Reputation points
    2021-09-11T02:04:49.537+00:00

    Thanks to everyone for the responses! (At least the ones I understood.)
    I think I may have found a crude-but-effective way I will try first; but if that doesn't pan out, I'll look at the XML and .json alternatives.

    It's this:
    Presently I store the "name" portion of all the listbox items as a single string in My.Settings separated by the ? character
    (which is not allowed in the name), and parse it with String.Split like this:
    ?banana?peach?pear becomes
    banana
    peach
    pear

    What I will try is to make the first character either a 1 or 0 for Checked or Unchecked, like
    ?0banana?0peach?1pear into
    0banana -> unchecked "banana"
    0peach -> unchecked "peach"
    1pear -> checked "pear"

    Seems like that ought to work; if not, I may be back. :-)

    0 comments No comments