CheckedListBox.CheckedItems Property
Assembly: System.Windows.Forms (in system.windows.forms.dll)
/** @property */ public CheckedItemCollection get_CheckedItems ()
public function get CheckedItems () : CheckedItemCollection
Not applicable.
Property Value
The CheckedListBox.CheckedItemCollection collection for the CheckedListBox.The collection is a subset of the objects in the Items collection, representing only those items whose System.Windows.Forms.CheckState is Checked or Indeterminate. The indexes in this collection are in ascending order.
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 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 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. foreach(int 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 show the object title and check state for each item selected. foreach(object itemChecked in checkedListBox1.CheckedItems) { // 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.Items.IndexOf(itemChecked)).ToString() + "."); } }
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 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.