ListBox.FindStringExact Método
Definição
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Localiza o primeiro item no ListBox que corresponde exatamente à cadeia de caracteres especificada.
Sobrecargas
| Nome | Description |
|---|---|
| FindStringExact(String) |
Localiza o primeiro item no ListBox que corresponde exatamente à cadeia de caracteres especificada. |
| FindStringExact(String, Int32) |
Localiza o primeiro item no ListBox que corresponde exatamente à cadeia de caracteres especificada. A pesquisa começa em um índice inicial específico. |
FindStringExact(String)
Localiza o primeiro item no ListBox que corresponde exatamente à cadeia de caracteres 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 ser pesquisado.
Retornos
O índice baseado em zero do primeiro item encontrado; retornará ListBox.NoMatches se nenhuma correspondência for encontrada.
Exemplos
O exemplo de código a seguir demonstra como usar o ListBox.FindStringExact método para pesquisar um ListBox controle para um item que corresponde exatamente a uma cadeia de caracteres especificada. Se nenhum item for encontrado que corresponda à cadeia de caracteres de pesquisa, FindStringExact retornará um valor -1 e o exemplo exibirá um MessageBox. Se for encontrado um item que corresponda ao texto de pesquisa, o exemplo usará o SetSelected método para selecionar o item no ListBox.
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
Comentários
A pesquisa executada por esse método não diferencia maiúsculas de minúsculas. A pesquisa procura uma correspondência exata com as palavras especificadas no parâmetro de cadeia de caracteres de pesquisa. s Você pode usar esse método para pesquisar o primeiro item que corresponde à cadeia de caracteres especificada. Em seguida, você pode executar tarefas como remover o item que contém o texto de pesquisa usando o Remove método ou alterar o texto do item. Depois de encontrar o texto especificado, se você quiser pesquisar outras instâncias do texto no ListBox. Você pode usar a versão do FindStringExact método que fornece um parâmetro para especificar um índice inicial dentro do ListBox. Se você quiser executar a pesquisa parcial de palavras em vez de uma correspondência exata de palavras, use o FindString método.
Confira também
Aplica-se a
FindStringExact(String, Int32)
Localiza o primeiro item no ListBox que corresponde exatamente à cadeia de caracteres especificada. A pesquisa começa em um í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 ser pesquisado.
- startIndex
- Int32
O índice baseado em zero do item antes do primeiro item a ser pesquisado. Defina como negativo um (-1) para pesquisar desde o início do controle.
Retornos
O índice baseado em zero do primeiro item encontrado; retornará ListBox.NoMatches se nenhuma correspondência for encontrada.
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 a seguir demonstra como usar o FindStringExact método para pesquisar todos os itens em um ListBox que corresponda exatamente ao texto de pesquisa especificado. O exemplo usa a versão do FindStringExact método que permite que você especifique um índice de pesquisa inicial do qual fazer uma pesquisa contínua de todos os itens no ListBox. O exemplo também demonstra como determinar quando o FindStringExact método começa a pesquisar na parte superior da lista depois de chegar à parte inferior da lista de itens, para impedir uma pesquisa recursiva. Depois que os itens são encontrados no ListBox, eles 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
Comentários
A pesquisa executada por esse método não diferencia maiúsculas de minúsculas. A pesquisa procura palavras que correspondam exatamente ao parâmetro de cadeia de caracteres de pesquisa especificado. s Você pode usar esse método para pesquisar o primeiro item que corresponde à cadeia de caracteres especificada no índice inicial especificado dentro da lista de itens para o ListBox. Em seguida, você pode executar tarefas como remover o item que contém o texto de pesquisa usando o Remove método ou alterar o texto do item. Esse método normalmente é usado depois que uma chamada é feita usando a versão desse método que não especifica um índice inicial. Depois que um item inicial é encontrado na lista, esse 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 você quiser executar uma pesquisa parcial de palavras em vez de uma correspondência de palavra exata, use o FindString método.
Note
Quando a pesquisa atinge a parte inferior da ListBox, ela continua pesquisando da parte superior da parte superior da parte de ListBox trás para o item especificado pelo startIndex parâmetro.