CheckedListBox.CheckedItemCollection (Clase)
Actualización: noviembre 2007
Encapsula la colección de elementos activados, incluidos los que se encuentran en un estado indeterminado, en un control CheckedListBox.
Ensamblado: System.Windows.Forms (en System.Windows.Forms.dll)
La colección de elementos activados es un subconjunto de todos los elementos del control CheckedListBox; sólo contiene los elementos que están activados o en un estado indeterminado.
La tabla siguiente es un ejemplo de la colección indizada de elementos del control (todos los elementos que contiene el control).
|
Índice |
Elemento |
Estado de activación |
|---|---|---|
|
0 |
objeto 1 |
Unchecked |
|
1 |
objeto 2 |
Checked |
|
2 |
objeto 3 |
Unchecked |
|
3 |
objeto 4 |
Indeterminate |
|
4 |
objeto 5 |
Checked |
Tomando como base el ejemplo anterior, en la tabla siguiente se muestra la colección indizada de los elementos activados.
|
Índice |
Elemento |
|---|---|
|
0 |
objeto 2 |
|
1 |
objeto 4 |
|
2 |
objeto 5 |
La clase CheckedListBox cuenta con dos miembros que le permiten obtener acceso a los índices almacenados: la propiedad Item y el método IndexOf.
Siguiendo con el ejemplo anterior, una llamada a la propiedad Item con un valor de parámetro 1 devuelve el objeto 4. Una llamada a IndexOf con un parámetro de objeto 4 devuelve un valor 1.
El ejemplo siguiente enumera los elementos activados en CheckedListBox.CheckedIndexCollection para ver el estado de activación de los elementos. El ejemplo muestra el uso del método GetItemCheckState para establecer el estado de activación de un elemento. En el ejemplo, se muestra también la forma utilizar la propiedad CheckedIndices para obtener CheckedListBox.CheckedIndexCollection y la forma de utilizar la propiedad CheckedItems para obtener la colección CheckedListBox.CheckedItemCollection.
El primer bucle utiliza el método GetItemCheckState para obtener el estado CheckState de cada elemento activado, a partir del índice del elemento. El segundo bucle también utiliza el método GetItemCheckState, pero se sirve del método ListBox.ObjectCollection.IndexOf para recuperar el índice del elemento.
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 Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.