Freigeben über


ListBox.SelectedIndices-Eigenschaft

Ruft eine Auflistung ab, die die nullbasierten Indizes aller derzeit ausgewählten Elemente in der ListBox enthält.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public ReadOnly Property SelectedIndices As SelectedIndexCollection
'Usage
Dim instance As ListBox
Dim value As SelectedIndexCollection

value = instance.SelectedIndices
public SelectedIndexCollection SelectedIndices { get; }
public:
property SelectedIndexCollection^ SelectedIndices {
    SelectedIndexCollection^ get ();
}
/** @property */
public SelectedIndexCollection get_SelectedIndices ()
public function get SelectedIndices () : SelectedIndexCollection

Eigenschaftenwert

Eine ListBox.SelectedIndexCollection, die die Indizes der derzeit im Steuerelement ausgewählten Elemente enthält. Wenn gegenwärtig keine Elemente ausgewählt sind, wird eine leere ListBox.SelectedIndexCollection zurückgegeben.

Hinweise

Für eine ListBox mit Mehrfachauswahl gibt diese Eigenschaft eine Auflistung mit den Indizes aller in der ListBox ausgewählten Elemente zurück. Für eine ListBox mit einfacher Auswahl gibt diese Eigenschaft eine Auflistung mit einem einzelnen Element zurück, das den Index des einzigen in der ListBox ausgewählten Elements enthält. Weitere Informationen zum Bearbeiten der Elemente der Auflistung finden Sie unter ListBox.SelectedIndexCollection.

Die ListBox-Klasse stellt eine Reihe von Möglichkeiten zum Verweisen auf ausgewählte Elemente bereit. Statt die Indexposition des derzeit ausgewählten Elements in einer ListBox mit einfacher Auswahl mithilfe der SelectedIndices-Eigenschaft abzurufen, können Sie auch die SelectedIndex-Eigenschaft verwenden. Wenn Sie das derzeit in der ListBox ausgewählte Element anstelle der Indexposition des Elements abrufen möchten, verwenden Sie die SelectedItem-Eigenschaft. Zusätzlich können Sie über die SelectedItems-Eigenschaft alle ausgewählten Elemente in einer ListBox mit Mehrfachauswahl abrufen.

Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie mithilfe der FindString-Methode in den Elementen der ListBox nach allen Instanzen des Suchtexts gesucht wird. Im Beispiel wird die Version der FindString-Methode verwendet, mit der Sie einen Startindex für die Suche angeben können, ab dem die Suche in allen Elementen in der ListBox fortgesetzt wird. Darüber hinaus wird veranschaulicht, wie festgestellt wird, dass die FindString-Methode die Suche am Anfang der Liste fortsetzt, nachdem das Ende der Liste erreicht wurde, sodass eine rekursive Suche vermieden wird. Wenn Elemente in der ListBox gefunden wurden, werden sie mithilfe der SetSelected-Methode ausgewählt.

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
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 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

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

ListBox-Klasse
ListBox-Member
System.Windows.Forms-Namespace
ListBox.SelectedIndex-Eigenschaft
SelectedItem
SelectedItems