CheckedListBox.CheckedIndexCollection Class
Assembly: System.Windows.Forms (in system.windows.forms.dll)
The checked indexes collection is a subset of the indexes into the collection of all items in the CheckedListBox control. These indexes specify items in a checked or indeterminate state.
The following table is an example of the indexed collection of items in the control (all items contained in the control).
| Index | Item | Check State |
|---|---|---|
| 0 | object 1 | |
| 1 | object 2 | |
| 2 | object 3 | Unchecked |
| 3 | object 4 | |
| 4 | object 5 | Checked |
Based on the previous example, the following table shows the indexed collection of indexes of checked items.
| Index | Index of Item |
|---|---|
| 0 | 1 |
| 1 | 3 |
| 2 | 4 |
The CheckedListBox class has two members that allow you to access the stored indexes, the Item property and the IndexOf method.
Based on the previous example, a call to the Item property with a parameter value of 1 returns a value of 3. A call to IndexOf with a parameter of 3 returns a value of 1.
The following example enumerates the checked items in the CheckedListBox.CheckedIndexCollection to see what check state an item is in. The example demonstrates using the GetItemCheckState method to set the check state of an item. The example also demonstrates using the CheckedIndices property to get the CheckedListBox.CheckedIndexCollection, and the CheckedItems property to get the CheckedListBox.CheckedItemCollection.
The first loop uses the GetItemCheckState method to get the CheckState of each checked item, given the index of the item. The second loop also uses GetItemCheckState, but uses the ListBox.ObjectCollection.IndexOf method to retrieve the index for the item.
Private Sub WhatIsChecked_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WhatIsChecked.Click ' Display in a message box all the items that are checked. Dim indexChecked As Integer Dim itemChecked As Object Const quote As String = """" ' First show the index and check state of all selected items. For Each indexChecked In CheckedListBox1.CheckedIndices ' The indexChecked variable contains the index of the item. MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" + _ CheckedListBox1.GetItemCheckState(indexChecked).ToString() + ".") Next ' Next show the object title and check state for each item selected. For Each itemChecked In CheckedListBox1.CheckedItems ' Use the IndexOf method to get the index of an item. MessageBox.Show("Item with title: " + quote + itemChecked.ToString() + quote + _ ", is checked. Checked state is: " + _ CheckedListBox1.GetItemCheckState(CheckedListBox1.Items.IndexOf(itemChecked)).ToString() + ".") Next End Sub
private void whatIsChecked_Click(Object sender, System.EventArgs e)
{
// Display in a message box all the items that are checked.
// First show the index and check state of all selected items.
IEnumerator objEnum = checkedListBox1.get_CheckedIndices().
GetEnumerator();
while (objEnum.MoveNext()) {
int indexChecked = (int)(Int32)(objEnum.get_Current());
// The indexChecked variable contains the index of the item.
MessageBox.Show("Index#: " + (Int32)indexChecked
+ ", is checked. Checked state is:"
+ checkedListBox1.GetItemCheckState(indexChecked).ToString()
+ ".");
}
// Next show the object title and check state for each item selected.
for (int iCtr = 0;
iCtr < checkedListBox1.get_CheckedItems().get_Count();
iCtr++) {
Object itemChecked =
checkedListBox1.get_CheckedItems().get_Item(iCtr);
// Use the IndexOf method to get the index of an item.
MessageBox.Show("Item with title: \"" + itemChecked.ToString()
+ "\", is checked. Checked state is: "
+ checkedListBox1.GetItemCheckState(
checkedListBox1.get_Items().IndexOf(itemChecked)).ToString()
+ ".");
}
} //whatIsChecked_Click
Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.