CheckedListBox.SetItemChecked(Int32, Boolean) Method

Definition

Sets CheckState for the item at the specified index to Checked.

public void SetItemChecked (int index, bool value);

Parameters

index
Int32

The index of the item to set the check state for.

value
Boolean

true to set the item as checked; otherwise, false.

Exceptions

The index specified is less than zero.

-or-

The index is greater than the count of items in the list.

Examples

The following example enumerates the items in the CheckedListBox and checks every other item in the list. The example demonstrates using the SetItemCheckState and SetItemChecked methods to set the check state of an item. For every other item that is to be checked, SetItemCheckState is called to set the CheckState to Indeterminate, while SetItemChecked is called on the other item to set the checked state to Checked.

The example also demonstrates using the Items property to get the CheckedListBox.ObjectCollection to get the Count of items.

private void CheckEveryOther_Click(object sender, System.EventArgs e) {
    // Cycle through every item and check every other.

    // Set flag to true to know when this code is being executed. Used in the ItemCheck
    // event handler.
    insideCheckEveryOther = true;

    for (int i = 0; i < checkedListBox1.Items.Count; i++) {
        // For every other item in the list, set as checked.
        if ((i % 2) == 0) {
            // But for each other item that is to be checked, set as being in an
            // indeterminate checked state.
            if ((i % 4) == 0)
                checkedListBox1.SetItemCheckState(i, CheckState.Indeterminate);
            else
                checkedListBox1.SetItemChecked(i, true);
        }
    }

    insideCheckEveryOther = false;
}

Remarks

When a value of true is passed, this method sets the CheckState value to Checked. A value of false sets CheckState to Unchecked.

Applies to

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9