Freigeben über


ListBox.SelectedIndexCollection-Klasse

Stellt die Auflistung dar, die die Indizes der in einem ListBox ausgewählten Elemente enthält.

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

Syntax

'Declaration
Public Class SelectedIndexCollection
    Implements IList, ICollection, IEnumerable
'Usage
Dim instance As SelectedIndexCollection
public class SelectedIndexCollection : IList, ICollection, IEnumerable
public ref class SelectedIndexCollection : IList, ICollection, IEnumerable
public class SelectedIndexCollection implements IList, ICollection, 
    IEnumerable
public class SelectedIndexCollection implements IList, ICollection, 
    IEnumerable

Hinweise

Die ListBox.SelectedIndexCollection-Klasse enthält die Indizes der in einem ListBox ausgewählten Elemente. Die im ListBox.SelectedIndexCollection gespeicherten Indizes sind Indexpositionen in der ListBox.ObjectCollection-Klasse. Die ListBox.ObjectCollection-Klasse enthält alle in der ListBox angezeigten Elemente.

Die folgende Tabelle ist ein Beispiel dafür, wie die ListBox.ObjectCollection die Elemente der ListBox sowie deren Auswahlzustand in einer Beispiel-ListBox speichert.

Index

Element

Auswahlzustand in ListBox

0

Objekt1

Nicht ausgewählt

1

Objekt2

Ausgewählt

2

Objekt3

Nicht ausgewählt

3

Objekt4

Ausgewählt

4

Objekt5

Ausgewählt

Auf der Grundlage des Beispiels für die ListBox.ObjectCollection in der vorausgegangenen Tabelle veranschaulicht die folgende Tabelle die Darstellung der ListBox.SelectedIndexCollection.

Index

Index des ausgewählten Elements in ObjectCollection

0

1

1

3

2

4

Mit den Methoden und Eigenschaften dieser Klasse können Sie eine Vielzahl von Aufgaben mit der Auflistung durchführen. Mit der Contains-Methode können Sie bestimmen, ob eine Indexposition in der ListBox.ObjectCollection-Klasse ein Member der ausgewählten Indizes ist, die in der ListBox.SelectedIndexCollection gespeichert sind. Sobald Sie festgestellt haben, dass das Element in der Auflistung vorhanden ist, können Sie mit der IndexOf-Methode bestimmen, wo eine bestimmte Indexposition in der ListBox.ObjectCollection für die ListBox gespeichert ist.

Beispiel

Das folgende Beispiel 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

Vererbungshierarchie

System.Object
  System.Windows.Forms.ListBox.SelectedIndexCollection

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, 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.SelectedIndexCollection-Member
System.Windows.Forms-Namespace