CheckedListBox Control

The CheckedListBox contains a number of items in the list. The user can select a box by checking each item. The Windows Form CheckedListBox control is a way to create longer, dynamic checklists.

Checked List Box Control C#

The CheckedListBox control of Windows forms extends the control of the ListBox. It does almost all we do in a list box and can also show a checkmark next to the items in the list.

Additional variations between the controls are that checked list boxes only support a DrawMode. Normal and only one item or none can be selected from checked list boxes.

Note: An item you have selected appears on the formula and is not the same as a checked item.

 

Syntax


public int Add (object item, bool isChecked);

 

With the Add method, we can include individual items in the list. Three states are supported in the object CheckedListBox:

Checked, Unchecked and Indeterminate by the CheckState list.


checkedListBox1.Items.Add("Sunday", CheckState.Checked);
checkedListBox1.Items.Add("Monday", CheckState.Unchecked);
checkedListBox1.Items.Add("Tuesday", CheckState.Indeterminate);

We can also add item by manual, using items property in Properties Windows and click on Collection, then create a individual items list.

Checked List Box Properties C#

Add Item at Runtime

To add objects to the list at runtime, assign the AddRange method to an array of object references. The default string value for each item will be displayed in the list.


string[] days = new[] { "Sunday", "Monday", "Tuesday" };
  checkedListBox1.Items.AddRange(days);

Note: The items are unchecked by default.

 

We must call SetItemChecked with the corresponding item if we want to check an item in the CheckedListBox.

Syntax


public void SetItemChecked (int index, bool value);

 

Parameters

index(Int32): The item index to specify the check state

value(Boolean): True to set the checked item ; false otherwise.

Change the values of the SetItemChecked method in true, if we want to check all items in the CheckedListbox.


string[] days = new[] { "Sunday", "Monday", "Tuesday" };
checkedListBox1.Items.AddRange(days);
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
    checkedListBox1.SetItemChecked(i, true);
}

 

Change the SetItemChecked value to false when we want all items to be unchecked in a CheckedListBox.

Checked List Box Control C#


for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
    checkedListBox1.SetItemChecked(i, false);
}

 

The CheckedListBox.ObjectCollection is used with the Items Property, to get the index of an item via the ListBox.ObjectCollection.IndexOfmethod.


private void button1_Click(object sender, EventArgs e)
        {
            foreach (int indexChecked in checkedListBox1.CheckedIndices)
            {
                MessageBox.Show("Index: " + indexChecked.ToString() + ", is checked. Checked state is:" +
                                checkedListBox1.GetItemCheckState(indexChecked).ToString() + ".");
            }
        }

 

Output:

Checked List Box Control C#

Checked List Box Control Source Code

Video Tutorial