ListBox.SelectedIndexCollection (Clase)
Actualización: noviembre 2007
Representa la colección que contiene los índices de los elementos seleccionados en un control ListBox.
Ensamblado: System.Windows.Forms (en System.Windows.Forms.dll)
La clase ListBox.SelectedIndexCollection almacena los índices de los elementos seleccionados en el control ListBox. Los índices almacenados en ListBox.SelectedIndexCollection son posiciones de índice de la clase ListBox.ObjectCollection. La clase ListBox.ObjectCollection almacena todos los elementos que se muestran en ListBox.
La siguiente tabla es un ejemplo de cómo ListBox.ObjectCollection almacena los elementos de ListBox así como su estado de selección en un control ListBox de ejemplo.
|
Índice |
Elemento |
Estado de selección dentro del control ListBox |
|---|---|---|
|
0 |
objeto1 |
No seleccionado |
|
1 |
objeto2 |
Seleccionado |
|
2 |
objeto3 |
No seleccionado |
|
3 |
objeto4 |
Seleccionado |
|
4 |
objeto5 |
Seleccionado |
De acuerdo con el ejemplo de ListBox.ObjectCollection de la tabla anterior, en la tabla siguiente se muestra cómo aparecerá ListBox.SelectedIndexCollection.
|
Índice |
Índice del elemento seleccionado en ObjectCollection |
|---|---|
|
0 |
1 |
|
1 |
3 |
|
2 |
4 |
Los métodos y propiedades de esta clase se pueden utilizar para realizar diversas tareas con la colección. El método Contains permite determinar si una posición de índice de la clase ListBox.ObjectCollection es miembro de los índices seleccionados almacenados en ListBox.SelectedIndexCollection. Cuando sepa que el elemento se encuentra en la colección, podrá utilizar el método IndexOf para determinar dónde se almacena una posición de índice específica en la clase ListBox.ObjectCollection del control ListBox.
En el siguiente ejemplo, se muestra la forma de utilizar el método FindString para buscar todas las instancias del texto de la búsqueda en los elementos del ListBox. En el ejemplo se utiliza la versión del método FindString que permite especificar un índice de búsqueda inicial a partir del cual se puede realizar una búsqueda continua de todos los elementos del control ListBox. En el ejemplo también se muestra la forma de determinar si el método FindString está iniciando la búsqueda desde el principio de la lista porque ya ha alcanzado el final de la lista de elementos, evitando así que se realicen búsquedas recursivas. Si se encuentran elementos en ListBox, se seleccionan con el método SetSelected.
private void FindAllOfMyString(string searchString) { // Set the SelectionMode property of the ListBox to select multiple items. listBox1.SelectionMode = SelectionMode.MultiExtended; // Set our intial index variable to -1. int x =-1; // If the search string is empty exit. if (searchString.Length != 0) { // Loop through and find each item that matches the search string. do { // Retrieve the item based on the previous index found. Starts with -1 which searches start. x = listBox1.FindString(searchString, x); // If no item is found that matches exit. if (x != -1) { // Since the FindString loops infinitely, determine if we found first item again and exit. if (listBox1.SelectedIndices.Count > 0) { if(x == listBox1.SelectedIndices[0]) return; } // Select the item in the ListBox once it is found. listBox1.SetSelected(x,true); } }while(x != -1); } }
private void FindAllOfMyString(String searchString)
{
// Set the SelectionMode property of the ListBox to
// select multiple items.
listBox1.set_SelectionMode(SelectionMode.MultiExtended);
// Set our intial index variable to -1.
int x = -1;
// If the search string is empty exit.
if (searchString.get_Length() != 0) {
// Loop through and find each item that matches the search string.
do {
// Retrieve the item based on the previous index found.
// Starts with -1 which searches start.
x = listBox1.FindString(searchString, x);
// If no item is found that matches exit.
if (x != -1) {
// Since the FindString loops infinitely, determine
// if we found first item again and exit.
if (listBox1.get_SelectedIndices().get_Count() > 0) {
if (x == listBox1.get_SelectedIndices().get_Item(0)) {
return;
}
}
// Select the item in the ListBox once it is found.
listBox1.SetSelected(x, true);
}
} while (x != -1);
}
} //FindAllOfMyString
} //Form1
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.