ListBox.FindStringExact Método

Definição

Encontra o primeiro item ListBox que corresponde exatamente à string especificada.

Sobrecargas

Name Description
FindStringExact(String)

Encontra o primeiro item ListBox que corresponde exatamente à string especificada.

FindStringExact(String, Int32)

Encontra o primeiro item ListBox que corresponde exatamente à string especificada. A pesquisa começa num índice inicial específico.

FindStringExact(String)

Encontra o primeiro item ListBox que corresponde exatamente à string especificada.

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

Parâmetros

s
String

O texto a procurar.

Devoluções

O índice em base zero do primeiro item encontrado; Retorna ListBox.NoMatches se não for encontrada correspondência.

Exemplos

O exemplo de código seguinte demonstra como usar o ListBox.FindStringExact método para pesquisar num ListBox controlo um item que corresponda exatamente a uma cadeia especificada. Se não forem encontrados itens que correspondam à cadeia de pesquisa, devolve FindStringExact um valor -1 e o exemplo mostra um MessageBox. Se for encontrado um item que corresponde ao texto da pesquisa, o exemplo utiliza o SetSelected método para selecionar o item no ListBoxarquivo .

private:
   void FindMySpecificString( 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->FindStringExact( searchString );

         // Determine if a valid index is returned. Select the item if it is valid.
         if ( index != ListBox::NoMatches )
                  listBox1->SetSelected( index, true );
         else
                  MessageBox::Show( "The search string did not find any items in the ListBox that exactly match the specified search string" );
      }
   }
private void FindMySpecificString(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.FindStringExact(searchString);
      // Determine if a valid index is returned. Select the item if it is valid.
      if (index != ListBox.NoMatches)
         listBox1.SetSelected(index,true);
      else
         MessageBox.Show("The search string did not find any items in the ListBox that exactly match the specified search string");
   }
}
Private Sub FindMySpecificString(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.FindStringExact(searchString)
      ' Determine if a valid index is returned. Select the item if it is valid.
      If index <> ListBox.NoMatches Then
         listBox1.SetSelected(index, True)
      Else
         MessageBox.Show("The search string did not find any items in the ListBox that exactly match the specified search string")
      End If
   End If
End Sub

Observações

A pesquisa realizada por este método não é sensível a maiúsculas minúsculas. A pesquisa procura uma correspondência exata com as palavras especificadas no parâmetro da cadeia de pesquisa, s. Podes usar este método para procurar o primeiro item que corresponda à string especificada. Pode então realizar tarefas como remover o item que contém o texto da pesquisa usando o Remove método ou alterar o texto do item. Depois de encontrar o texto especificado, se quiser procurar outras instâncias do texto no ListBox, pode usar a versão do FindStringExact método que fornece um parâmetro para especificar um índice inicial dentro do ListBox. Se quiser realizar uma pesquisa parcial em vez de uma correspondência exata, use o FindString método.

Ver também

Aplica-se a

FindStringExact(String, Int32)

Encontra o primeiro item ListBox que corresponde exatamente à string especificada. A pesquisa começa num índice inicial específico.

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

Parâmetros

s
String

O texto a procurar.

startIndex
Int32

O índice base zero do item antes do primeiro item a ser pesquisado. Defina para menos um (-1) para pesquisar desde o início do controlo.

Devoluções

O índice em base zero do primeiro item encontrado; Retorna ListBox.NoMatches se não for encontrada correspondência.

Exceções

O startIndex parâmetro é menor que zero ou maior ou igual ao valor da Count propriedade da ListBox.ObjectCollection classe.

Exemplos

O exemplo de código seguinte demonstra como usar o FindStringExact método para procurar todos os itens em um ListBox que correspondam exatamente ao texto de pesquisa especificado. O exemplo utiliza a versão do FindStringExact método que permite especificar um índice de pesquisa inicial a partir do qual fazer uma pesquisa contínua de todos os itens do ListBox. O exemplo também demonstra como determinar quando o FindStringExact método começa a procurar no topo da lista depois de chegar ao fundo da lista de itens, para evitar uma pesquisa recursiva. Uma vez que os itens são encontrados no ListBox, são selecionados usando o SetSelected método.

private:
   void FindAllOfMyExactStrings( 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->FindStringExact( searchString, x );

            // If no item is found that matches exit.
            if ( x != -1 )
            {
               // Since the FindStringExact 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 FindAllOfMyExactStrings(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.FindStringExact(searchString, x);
         // If no item is found that matches exit.
         if (x != -1)
         {
            // Since the FindStringExact 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 FindAllOfMyExactStrings(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.FindStringExact(searchString, x)
         ' If no item is found that matches exit.
         If x <> -1 Then
            ' Since the FindStringExact 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

Observações

A pesquisa realizada por este método não é sensível a maiúsculas minúsculas. A pesquisa procura palavras que correspondam exatamente ao parâmetro especificado da cadeia de pesquisa, s. Pode usar este método para procurar o primeiro item que corresponde à string especificada no índice inicial especificado dentro da lista de itens para o ListBox. Pode então realizar tarefas como remover o item que contém o texto da pesquisa usando o Remove método ou alterar o texto do item. Este método é normalmente usado após uma chamada ter sido feita usando a versão deste método que não especifica um índice inicial. Uma vez encontrado um item inicial na lista, este método é normalmente usado para encontrar mais instâncias do texto de pesquisa, especificando a posição do índice no startIndex parâmetro do item após a primeira instância encontrada do texto de pesquisa. Se quiser fazer uma pesquisa parcial em vez de uma correspondência exata, use o FindString método.

Note

Quando a pesquisa chega ao fundo do ListBox, continua a procurar do topo do verso ListBox até ao item especificado pelo startIndex parâmetro.

Ver também

Aplica-se a