How to programmatically check an item in a CheckedListBox in C#?

Suppose you want to check the item on clicking a button.

private void button1_Click(object sender, EventArgs e)
{
    checkedListBox1.SetItemChecked(itemIndex, true);
}

Where itemIndex is the index of the item to be checked, it starts from 0.


You need to call SetItemChecked with the relevant item.

The documentation for CheckedListBox.ObjectCollection has an example which checks every other item in a collection.


This is how you can select/tick or deselect/untick all of the items at once:

private void SelectAllCheckBoxes(bool CheckThem) {
    for (int i = 0; i <= (checkedListBox1.Items.Count - 1); i++) {
        if (CheckThem)
        {
            checkedListBox1.SetItemCheckState(i, CheckState.Checked);
        }
        else
        {
            checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
        }
    }  
}

In my program I've used the following trick:

CheckedListBox.SetItemChecked(CheckedListBox.Items.IndexOf(Item),true);

How does things works:
SetItemChecked(int index, bool value) is method which sets the exact checked state at the specific item. You have to specify index of item You want to check (use IndexOf method, as an argument specify text of item) and checked state (true means item is checked, false unchecked).
This method runs through all items in CheckedListBox and checks (or unchecks) the one with specified index.
For example, a short piece of my code - FOREACH cycle runs through specified program names, and if the program is contained in CheckedLitBox (CLB...), checks it:

string[] ProgramNames = sel_item.SubItems[2].Text.Split(';');
foreach (string Program in ProgramNames)
{
    if (edit_mux.CLB_ContainedPrograms.Items.Contains(Program))
        edit_mux.CLB_ContainedPrograms.SetItemChecked(edit_mux.CLB_ContainedPrograms.Items.IndexOf(Program), true);
}