CheckedListBox.CheckedIndexCollection (Clase)
Actualización: noviembre 2007
Encapsula la colección de índices de elementos activados, incluidos los elementos que se encuentran en un estado indeterminado, en un control CheckedListBox.
Ensamblado: System.Windows.Forms (en System.Windows.Forms.dll)
La colección de índices activados es un subconjunto de los índices de la colección de todos los elementos del control CheckedListBox. Estos índices especifican 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 |
Item |
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 índices de elementos activados.
|
Índice |
Índice de elemento |
|---|---|
|
0 |
1 |
|
1 |
3 |
|
2 |
4 |
La clase CheckedListBox tiene dos miembros que permiten tener acceso a los índices almacenados, a saber, 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 valor 3. Una llamada a IndexOf con un parámetro 3 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 CheckState de cada elemento activado, dado el índice del elemento. El segundo bucle también utiliza GetItemCheckState, pero utiliza el 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.