locked
CheckedListBox Checkstate Save RRS feed

  • Question

  • Okay I am now using a CheckedListBox and I was wondering if there is a way to save the checkstate of the items? Its very easy with checkboxes but I have not found out how to do it with this.
    Monday, September 6, 2010 9:50 AM

Answers

  • Hello Hurtz44,

    Thanks for your post.

    You could use the GetItemCheckState method to set the check state of an item. Check the following example which enumerates the checked items in the CheckedListBox.CheckedIndexCollection to see what check state an item is in. Hope it could be helpful to you.

     Private Sub CheckedListBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedListBox1.Click
            ' Display in a message box all the items that are checked.
            Dim indexChecked As Integer
            Dim itemChecked As Object
            Const quote As String = """"

            ' First show the index and check state of all selected items.
            For Each indexChecked In CheckedListBox1.CheckedIndices
                ' The indexChecked variable contains the index of the item.
                MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" + _
                                CheckedListBox1.GetItemCheckState(indexChecked).ToString() + ".")
            Next

            ' Next show the object title and check state for each item selected.
            For Each itemChecked In CheckedListBox1.CheckedItems

                ' Use the IndexOf method to get the index of an item.
                MessageBox.Show("Item with title: " + quote + itemChecked.ToString() + quote + _
                                ", is checked. Checked state is: " + _
                                CheckedListBox1.GetItemCheckState(CheckedListBox1.Items.IndexOf(itemChecked)).ToString() + ".")
            Next

        End Sub

    More infromation, please check:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.checkeditemcollection.aspx
    (CheckedListBox.CheckedItemCollection Class)

    If you have any problems, please feel free to follow up.

    Best regards,
    Liliane
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. Thanks
    • Marked as answer by Liliane Teng Wednesday, September 15, 2010 9:17 AM
    Friday, September 10, 2010 8:40 AM

All replies

  • Give us a piece of code how you do it with a checkbox, because there are endless posibilities to do what you want: DataBase, Setting Config file, own XML, Registry, Serializing, CSV, and so on.

     


    Success
    Cor
    Monday, September 6, 2010 10:19 AM
  • Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked Then
          My.Settings.CheckSave = CheckBox1.CheckState
          My.Settings.Save()
        End If
      End Sub
    
    but this is entirely different as they dont have a simple "CheckState" command like they do with checkboxes so it complicates what I am trying to do
    Monday, September 6, 2010 10:40 AM
  • But i need it to save the Checkstate of Every Item I have in the CheckedListBox
    Monday, September 6, 2010 10:42 AM
  • Hello Hurtz44,

    Thanks for your post.

    You could use the GetItemCheckState method to set the check state of an item. Check the following example which enumerates the checked items in the CheckedListBox.CheckedIndexCollection to see what check state an item is in. Hope it could be helpful to you.

     Private Sub CheckedListBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedListBox1.Click
            ' Display in a message box all the items that are checked.
            Dim indexChecked As Integer
            Dim itemChecked As Object
            Const quote As String = """"

            ' First show the index and check state of all selected items.
            For Each indexChecked In CheckedListBox1.CheckedIndices
                ' The indexChecked variable contains the index of the item.
                MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" + _
                                CheckedListBox1.GetItemCheckState(indexChecked).ToString() + ".")
            Next

            ' Next show the object title and check state for each item selected.
            For Each itemChecked In CheckedListBox1.CheckedItems

                ' Use the IndexOf method to get the index of an item.
                MessageBox.Show("Item with title: " + quote + itemChecked.ToString() + quote + _
                                ", is checked. Checked state is: " + _
                                CheckedListBox1.GetItemCheckState(CheckedListBox1.Items.IndexOf(itemChecked)).ToString() + ".")
            Next

        End Sub

    More infromation, please check:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.checkeditemcollection.aspx
    (CheckedListBox.CheckedItemCollection Class)

    If you have any problems, please feel free to follow up.

    Best regards,
    Liliane
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. Thanks
    • Marked as answer by Liliane Teng Wednesday, September 15, 2010 9:17 AM
    Friday, September 10, 2010 8:40 AM
  • Hello Hurtz44,

    When you come back, please have a try. If you think it provides no help, please unmark and follow up. Thanks.

    Best regards,
    Liliane
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. Thanks
    • Proposed as answer by PyoShong Monday, April 28, 2014 10:37 PM
    • Unproposed as answer by PyoShong Monday, April 28, 2014 10:37 PM
    Wednesday, September 15, 2010 9:19 AM
  • It is not obvious to save the checkstate of each items in CheckedListBox because Items property is not serializable.  However, we can still use the application settings.  Here is what I did.

    Summary : I happen to have 16 items in the list. I added a variable in Settings in uint type and used each bit to store the checkState.

    1. Declare a variable to      represent the items in Project > settings > Name =      cklbSettings, Type = uint, Scope=user, Value 65535
    2. Load setting :  
        
      for (int i      = 0 ; i < checkedLBSomething.Items.Count ; i ++)

    {

        checkedLBSomething.SetItemCheckState(i, (CheckState)((Settings.Default.cklbSettings>> i) & 1));

    }

    1. Save Setting

                uint num = 0;

                Properties.Settings.Default.cklbDefectTypeIndices = 0;

                for (int i = 0; i < checkedLBSomething.Items.Count; i++)

                {

                    num = (uint) checkedLBSomething.GetItemCheckState(i);

                    Properties.Settings.Default.cklbSettings|= num << i;

                }

                Properties.Settings.Default.Save();

    Monday, April 28, 2014 11:14 PM