ListBox.SelectedIndexCollection.Count Propiedad

Definición

Obtiene el número de elementos de la colección.Gets the number of items in the collection.

public:
 property int Count { int get(); };
[System.ComponentModel.Browsable(false)]
public int Count { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Count : int
Public ReadOnly Property Count As Integer

Valor de propiedad

Int32

Número de elementos de la colección.The number of items in the collection.

Implementaciones

Atributos

Ejemplos

En el ejemplo siguiente se muestra cómo utilizar el FindString método para buscar todas las instancias del texto de búsqueda en los elementos de ListBox .The following example demonstrates how to use the FindString method to search for all instances of the search text in the items of the ListBox. En el ejemplo se usa la versión del FindString método que permite especificar un índice de búsqueda inicial desde el que realizar una búsqueda continua de todos los elementos de ListBox .The example uses the version of the FindString method that enables you to specify a starting search index from which to do a continual search of all items in the ListBox. En el ejemplo también se muestra cómo determinar cuándo FindString comienza a buscar el método desde la parte superior de la lista después de llegar a la parte inferior de la lista de elementos para evitar una búsqueda recursiva.The example also demonstrates how to determine when the FindString method begins searching from the top of the list after it reaches the bottom of the list of items to prevent a recursive search. Una vez que los elementos se encuentran en ListBox , se seleccionan mediante el SetSelected método.Once items are found in the ListBox, they are selected using the SetSelected method.

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.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 Sub FindAllOfMyString(ByVal searchString As String)
   ' Set the SelectionMode property of the ListBox to select multiple items.
   listBox1.SelectionMode = SelectionMode.MultiExtended

   ' Set our intial index variable to -1.
   Dim x As Integer = -1
   ' If the search string is empty exit.
   If searchString.Length <> 0 Then
      ' 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 Then
            ' Since the FindString loops infinitely, determine if we found first item again and exit.
            If ListBox1.SelectedIndices.Count > 0 Then
               If x = ListBox1.SelectedIndices(0) Then
                  Return
               End If
            End If
            ' Select the item in the ListBox once it is found.
            ListBox1.SetSelected(x, True)
         End If
      Loop While x <> -1
   End If
End Sub

Comentarios

Esta propiedad le permite determinar el número de elementos seleccionados en ListBox .This property enables you to determine the number of selected items in the ListBox. Después, puede usar este valor al recorrer en bucle los valores de la colección y debe proporcionar un número de iteraciones para realizar el bucle.You can then use this value when looping through the values of the collection and you need to provide a number of iterations to perform the loop. A menos que la SelectionMode propiedad de ListBox esté establecida en SelectionMode.MultiSimple o SelectionMode.MultiExtended , esta propiedad siempre devuelve un valor de cero (0) o uno (1) en función de si tiene un elemento seleccionado.Unless the SelectionMode property of the ListBox is set to SelectionMode.MultiSimple or SelectionMode.MultiExtended, this property always returns a value of zero (0) or one (1) depending on whether you have a selected item.

Se aplica a