Freigeben über


ListBox.SelectedIndexCollection.Count Eigenschaft

Definition

Ruft die Anzahl der Elemente in der Auflistung ab.

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

Eigenschaftswert

Die Anzahl der Elemente in der Auflistung.

Implementiert

Attribute

Beispiele

Im folgenden Beispiel wird veranschaulicht, wie FindString die Methode verwendet wird, um nach allen Instanzen des Suchtexts in den Elementen des Suchtexts ListBoxzu suchen. Im Beispiel wird die Version der Methode verwendet, mit der FindString Sie einen Startsuchindex angeben können, aus dem eine kontinuierliche Suche aller Elemente in der ListBoxDatei ausgeführt werden soll. Das Beispiel veranschaulicht außerdem, wie Sie bestimmen können, wann die FindString Methode mit der Suche vom Anfang der Liste beginnt, nachdem sie das Ende der Liste der Elemente erreicht hat, um eine rekursive Suche zu verhindern. Sobald Elemente in der ListBoxDatei gefunden wurden, werden sie mithilfe der SetSelected Methode ausgewählt.

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

Hinweise

Mit dieser Eigenschaft können Sie die Anzahl der ausgewählten Elemente in der ListBox. Sie können diesen Wert dann verwenden, wenn Sie die Werte der Auflistung durchlaufen und eine Reihe von Iterationen angeben müssen, um die Schleife auszuführen. Sofern die SelectionMode Eigenschaft der ListBox Eigenschaft nicht auf SelectionMode.MultiSimple oder SelectionMode.MultiExtended, gibt diese Eigenschaft immer einen Wert von Null (0) oder 1 (1) zurück, je nachdem, ob Sie ein ausgewähltes Element haben.

Gilt für: