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

I have a CheckedListBox, and I want to automatically tick one of the items in it.

The CheckedItems collection doesn't allow you to add things to it.

Any suggestions?


Solution 1:

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.

Solution 2:

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);
        }
    }  
}