ListBox.SelectedIndices Propriété

Définition

Obtient une collection qui contient les index de base zéro de tous les éléments actuellement sélectionnés dans 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

Valeur de propriété

ListBox.SelectedIndexCollection

ListBox.SelectedIndexCollection contenant les index des éléments actuellement sélectionnés dans le contrôle. Si aucun élément n'est sélectionné, un ListBox.SelectedIndexCollection vide est retourné.

Attributs

Exemples

L’exemple de code suivant montre comment utiliser la FindString méthode pour rechercher toutes les instances du texte de recherche dans les éléments du ListBox. L’exemple utilise la version de la FindString méthode qui vous permet de spécifier un index de recherche de départ à partir duquel effectuer une recherche continue de tous les éléments dans le ListBox. L’exemple montre également comment déterminer quand la FindString méthode commence à effectuer une recherche en haut de la liste une fois qu’elle atteint le bas de la liste des éléments pour empêcher une recherche récursive. Une fois que les éléments sont trouvés dans le ListBox, ils sont sélectionnés à l’aide de la SetSelected méthode.

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

Remarques

Pour une sélection ListBoxmultiple, cette propriété renvoie une collection contenant les index à tous les éléments sélectionnés dans le ListBox. Pour une sélection ListBoxunique, cette propriété retourne une collection contenant un seul élément contenant l’index du seul élément sélectionné dans le ListBox. Pour plus d’informations sur la manipulation des éléments de la collection, consultez ListBox.SelectedIndexCollection.

La ListBox classe fournit plusieurs façons de référencer les éléments sélectionnés. Au lieu d’utiliser la SelectedIndices propriété pour obtenir la position d’index de l’élément actuellement sélectionné dans une sélection ListBoxunique, vous pouvez utiliser la SelectedIndex propriété. Si vous souhaitez obtenir l’élément actuellement sélectionné dans le ListBoxfichier , au lieu de la position d’index de l’élément, utilisez la SelectedItem propriété. En outre, vous pouvez utiliser la SelectedItems propriété si vous souhaitez obtenir tous les éléments sélectionnés dans une sélection ListBoxmultiple.

S’applique à

Voir aussi