ListBox.FindString Metodo

Definizione

Trova il primo elemento nell'oggetto ListBox che inizia con la stringa specificata.

Overload

Nome Descrizione
FindString(String)

Trova il primo elemento nell'oggetto ListBox che inizia con la stringa specificata.

FindString(String, Int32)

Trova il primo elemento nell'oggetto ListBox che inizia con la stringa specificata. La ricerca inizia in corrispondenza di un indice iniziale specifico.

FindString(String)

Trova il primo elemento nell'oggetto ListBox che inizia con la stringa specificata.

public:
 int FindString(System::String ^ s);
public int FindString(string s);
member this.FindString : string -> int
Public Function FindString (s As String) As Integer

Parametri

s
String

Testo da cercare.

Valori restituiti

Indice in base zero del primo elemento trovato; restituisce ListBox.NoMatches se non viene trovata alcuna corrispondenza.

Eccezioni

Il valore del s parametro è minore di -1 o maggiore o uguale al numero di elementi.

Esempio

Nell'esempio di codice seguente viene illustrato come usare il FindString metodo per cercare la prima istanza di una stringa in un oggetto ListBox. Se non viene trovato alcun elemento che corrisponde alla stringa FindString di ricerca restituisce un valore -1 e nell'esempio viene visualizzato un oggetto MessageBox. Se viene trovato un elemento che corrisponde al testo di ricerca, nell'esempio viene usato il SetSelected metodo per selezionare l'elemento in ListBox.

private:
   void FindMyString( String^ searchString )
   {
      // Ensure we have a proper string to search for.
      if ( searchString != String::Empty )
      {
         // Find the item in the list and store the index to the item.
         int index = listBox1->FindString( searchString );

         // Determine if a valid index is returned. Select the item if it is valid.
         if ( index != -1 )
                  listBox1->SetSelected( index, true );
         else
                  MessageBox::Show( "The search string did not match any items in the ListBox" );
      }
   }
private void FindMyString(string searchString)
{
   // Ensure we have a proper string to search for.
   if (!string.IsNullOrEmpty(searchString))
   {
      // Find the item in the list and store the index to the item.
      int index = listBox1.FindString(searchString);
      // Determine if a valid index is returned. Select the item if it is valid.
      if (index != -1)
         listBox1.SetSelected(index,true);
      else
         MessageBox.Show("The search string did not match any items in the ListBox");
   }
}
Private Sub FindMyString(ByVal searchString As String)
   ' Ensure we have a proper string to search for.
   If searchString <> String.Empty Then
      ' Find the item in the list and store the index to the item.
      Dim index As Integer = listBox1.FindString(searchString)
      ' Determine if a valid index is returned. Select the item if it is valid.
      If index <> -1 Then
         listBox1.SetSelected(index, True)
      Else
         MessageBox.Show("The search string did not match any items in the ListBox")
      End If
   End If
End Sub

Commenti

La ricerca eseguita da questo metodo non fa distinzione tra maiuscole e minuscole. La ricerca cerca parole che corrispondono parzialmente al parametro della stringa di ricerca specificato, s. È possibile utilizzare questo metodo per cercare il primo elemento che corrisponde alla stringa specificata. È quindi possibile eseguire attività come la rimozione dell'elemento che contiene il testo di ricerca usando il Remove metodo o modificando il testo dell'elemento. Dopo aver trovato il testo specificato, se si desidera cercare altre istanze del testo in ListBox, è possibile usare la versione del FindString metodo che fornisce un parametro per specificare un indice iniziale all'interno di ListBox. Se si desidera eseguire una ricerca per una corrispondenza esatta di parola anziché una corrispondenza parziale, usare il FindStringExact metodo .

Vedi anche

Si applica a

FindString(String, Int32)

Trova il primo elemento nell'oggetto ListBox che inizia con la stringa specificata. La ricerca inizia in corrispondenza di un indice iniziale specifico.

public:
 int FindString(System::String ^ s, int startIndex);
public int FindString(string s, int startIndex);
member this.FindString : string * int -> int
Public Function FindString (s As String, startIndex As Integer) As Integer

Parametri

s
String

Testo da cercare.

startIndex
Int32

Indice in base zero dell'elemento prima della ricerca del primo elemento. Impostare su negativo (-1) per eseguire la ricerca dall'inizio del controllo.

Valori restituiti

Indice in base zero del primo elemento trovato; restituisce ListBox.NoMatches se non viene trovata alcuna corrispondenza.

Eccezioni

Il startIndex parametro è minore di zero o maggiore o uguale al valore della Count proprietà della ListBox.ObjectCollection classe .

Esempio

Nell'esempio di codice seguente viene illustrato come usare il FindString metodo per cercare tutte le istanze del testo di ricerca negli elementi di ListBox. Nell'esempio viene utilizzata la versione del FindString metodo che consente di specificare un indice di ricerca iniziale da cui eseguire una ricerca continua di tutti gli elementi in ListBox. Nell'esempio viene inoltre illustrato come determinare quando il metodo inizia la FindString ricerca dalla parte superiore dell'elenco dopo che raggiunge la fine dell'elenco di elementi per impedire una ricerca ricorsiva. Dopo aver trovato gli elementi in ListBox, vengono selezionati usando il SetSelected metodo .

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

Commenti

La ricerca eseguita da questo metodo non fa distinzione tra maiuscole e minuscole. La ricerca cerca parole che corrispondono parzialmente al parametro della stringa di ricerca specificato, s. È possibile utilizzare questo metodo per cercare il primo elemento che corrisponde alla stringa specificata in corrispondenza dell'indice iniziale specificato all'interno dell'elenco di elementi per .ListBox È quindi possibile eseguire attività come la rimozione dell'elemento che contiene il testo di ricerca usando il Remove metodo o modificando il testo dell'elemento. Questo metodo viene in genere utilizzato dopo che è stata effettuata una chiamata utilizzando la versione di questo metodo che non specifica un indice iniziale. Dopo aver trovato un elemento iniziale nell'elenco, questo metodo viene in genere usato per trovare altre istanze del testo di ricerca specificando la posizione di indice nel startIndex parametro dell'elemento dopo la prima istanza trovata del testo di ricerca. Se si desidera eseguire una ricerca per una corrispondenza esatta di parola anziché una corrispondenza parziale, usare il FindStringExact metodo .

Annotazioni

Quando la ricerca raggiunge la parte inferiore di ListBox, continua la ricerca dalla parte superiore dell'elemento ListBox all'elemento specificato dal startIndex parametro .

Vedi anche

Si applica a