C# – How to use CheckedListBox control?

CheckedListBox control allows selecting multiple options from the ListBox. It is basically a ListBox control; with the additional feature, in which a check box is displayed for each item to allow the user to select the option. ListBox control allows us to select multiple items from the list; but, in CheckedListBox , we call it multiple selections of options. Because you need to check the checkboxes in order to select the items.

Why CheckedListBox control,  we already have ListBox control which allows multi-selection? This control is more user-friendly. And it maintains three states for each option; checked, indeterminate and unchecked which is not possible with ListBox control.

I recommend you to read, “C# – How to use ListBox control?“, before you continue with the Article.

Properties

We can add or remove the items to or from the CheckedListBox control using its Items property; which is a collection.

By default, when selecting an item from the control, the check box associated with the item will not be selected. This can be controlled by using CheckOnClick property. When setting this property to True, the check box is also checked or unchecked when selecting the item. Otherwise, we need first select the item, and then click again to check or uncheck the item.

We can control the way the items are displayed in the control vertically or in multi-columns by setting its MultiColumn property. When setting this property to True, ListBox items will be displayed in columns horizontally.

By setting the Sorted property to True we can sort the list of items.

Note that, this control provides checkboxes to allow us to select multiple items; hence SelectionMode property should always be set with the value One or None; MultiSimple and MultiExtended values are invalid for this control. When attempting to set SelectionMode property with these multi-selection values, it will throw the “Multi-selection is not supported on CheckedListBox.” exception.

When the items are selected, theCheckedIndices property is set with a list of selected item indexes. And CheckedItems property is set with a list of selected items.

Note that, in ListBox control, we use SelectedIndices and SelectedItems properties to get the list of selected items indexes and selected items respectively.  For CheckedListBox control, we cannot use these properties, as multi-selection is not allowed in SelectionMode property. Hence we need to use checked versions of these properties.

Methods

To add or remove items to or from CheckedListBox control, we need to add or remove them through its Items property. Using Items.Add(object) method, we can add items to the control. We can use Items.Remove(object) method to remove the item and Items.RemoveAt(int) method to remove the item from the specified index. Items.Clear() method to clear all items from the CheckedListBox control.

GetItemChecked(int) method is used to check whether the item at the specified index is checked or not. GetItemCheckState(int) method is used to check the check box state of the item at the specified index; it has three states, Checked, Indeterminate and Unchecked. (I recommend reading “C# – How to use CheckBox control?“, to know more about the check box states.)

Events

Whenever the check state of the item is about to change, ItemCheck event will be raised and ItemCheckEventArgs object filled with Index value of the item, CurrentValue and NewValue of the item’s check state. After the event handler execution, the item’s state will be updated.

SelectedIndexChanged event occurs when the value of SelectedIndex or SelectedIndicies property has changed.

Working Example

Let’s put all together what we learned so far; and here is the complete working code.

The screenshot of this program looks like this below;

CheckedListBox control demo
CheckedListBox control demo

(Raju)

C# – How to use CheckedListBox control?

One thought on “C# – How to use CheckedListBox control?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top