CheckedListBox control allows to select multiple options from the ListBox. It is basically a ListBox control; with additional feature that, 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 as multiple selection of options. Because, you need to check the check boxes 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.
We can add or remove the items to or from the CheckedListBox control using it’s Items property; which is a collection.
By default, when select 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 set this property to True, the check box also checked or unchecked when select 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 in vertically or in multi columns by setting it’s MultiColumn property. When set this property to True, ListBox items will be displayed in columns horizontally.
By setting the Sorted property to True we can sort the list items.
Note that, this control provides check boxes to allow us to select multiple items; henceSelectionMode property should always set with the value One or None; MultiSimple and MultiExtended values are invalid for this control. When attempt 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, it’s CheckedIndices property is set with list of selected items indexes. And CheckedItems property is set with 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.
To add or remove items to or from CheckedListBox control, we need to add or remove them through it’sItems 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 to read “C# – How to use CheckBox control?“, to know more about the check box states.)
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, item’s state will be updated.
SelectedIndexChanged event occurs when the value of SelectedIndex or SelectedIndicies property has changed.
Let’s put all together what we learned so far; and here is the complete working code.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.