ListBox.SelectedIndices Właściwość

Definicja

Pobiera kolekcję zawierającą indeksy zerowe wszystkich aktualnie wybranych elementów w obiekcie ListBox.

public:
 property System::Windows::Forms::ListBox::SelectedIndexCollection ^ SelectedIndices { System::Windows::Forms::ListBox::SelectedIndexCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.ListBox.SelectedIndexCollection SelectedIndices { get; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectedIndices : System.Windows.Forms.ListBox.SelectedIndexCollection
Public ReadOnly Property SelectedIndices As ListBox.SelectedIndexCollection

Wartość właściwości

ListBox.SelectedIndexCollection

Element ListBox.SelectedIndexCollection zawierający indeksy aktualnie wybranych elementów w kontrolce. Jeśli obecnie nie wybrano żadnych elementów, zwracana jest pusta ListBox.SelectedIndexCollection wartość.

Atrybuty

Przykłady

Poniższy przykład kodu pokazuje, jak za pomocą FindString metody wyszukać wszystkie wystąpienia tekstu wyszukiwania w elementach elementu ListBox. W przykładzie użyto wersji FindString metody, która umożliwia określenie początkowego indeksu wyszukiwania, z którego ma być stale wyszukiwane wszystkie elementy w elemecie ListBox. W przykładzie pokazano również, jak określić, kiedy FindString metoda rozpoczyna wyszukiwanie w górnej części listy po osiągnięciu dolnej części listy elementów, aby zapobiec wyszukiwaniu cyklicznego. Po znalezieniu elementów w elemecie ListBoxsą one wybrane przy użyciu SetSelected metody .

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

Uwagi

W przypadku wielokrotnego zaznaczenia ListBoxta właściwość zwraca kolekcję zawierającą indeksy do wszystkich elementów wybranych w obiekcie ListBox. W przypadku pojedynczego zaznaczenia ListBoxta właściwość zwraca kolekcję zawierającą pojedynczy element zawierający indeks jedynego wybranego elementu w elemencie ListBox. Aby uzyskać więcej informacji na temat manipulowania elementami kolekcji, zobacz ListBox.SelectedIndexCollection.

Klasa ListBox udostępnia wiele sposobów odwołowania się do wybranych elementów. Zamiast używać SelectedIndices właściwości w celu uzyskania pozycji indeksu aktualnie wybranego elementu w jednym zaznaczeniu ListBox, można użyć SelectedIndex właściwości . Jeśli chcesz uzyskać element, który jest aktualnie wybrany w elemencie ListBox, zamiast pozycji indeksu elementu, użyj SelectedItem właściwości . Ponadto można użyć SelectedItems właściwości , jeśli chcesz uzyskać wszystkie wybrane elementy w wielokrotnym zaznaczeniu ListBox.

Dotyczy

Zobacz też