SetItemCheckState Method

CheckedListBox.SetItemCheckState Method

Sets the check state of the item at the specified index.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public void SetItemCheckState (
	int index,
	CheckState value
)
public void SetItemCheckState (
	int index, 
	CheckState value
)
public function SetItemCheckState (
	index : int, 
	value : CheckState
)
Not applicable.

Parameters

index

The index of the item to set the state for.

value

One of the CheckState values.

Exception typeCondition

ArgumentOutOfRangeException

The index specified is less than zero.

-or-

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

InvalidEnumArgumentException

The value is not one of the CheckState values.

The SetItemCheckState method raises the ItemCheck event.

Items whose CheckState is set to Indeterminate appear with a check mark in the check box, but the box is grayed to indicate the indeterminate status of the checked item.

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

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.get_Items().get_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;
} //checkEveryOther_Click

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2016 Microsoft