ListBox.FindStringExact 方法

定义

查找 ListBox 中第一个精确匹配指定字符串的项。Finds the first item in the ListBox that exactly matches the specified string.

重载

FindStringExact(String)

查找 ListBox 中第一个精确匹配指定字符串的项。Finds the first item in the ListBox that exactly matches the specified string.

FindStringExact(String, Int32)

查找 ListBox 中第一个精确匹配指定字符串的项。Finds the first item in the ListBox that exactly matches the specified string. 搜索从特定的起始索引处开始。The search starts at a specific starting index.

FindStringExact(String)

查找 ListBox 中第一个精确匹配指定字符串的项。Finds the first item in the ListBox that exactly matches the specified string.

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

参数

s
String

要搜索的文本。The text to search for.

返回

找到的第一个项的从零开始的索引;如果找不到匹配项,则返回 ListBox.NoMatchesThe zero-based index of the first item found; returns ListBox.NoMatches if no match is found.

示例

下面的代码示例演示如何使用 ListBox.FindStringExact 方法在 ListBox 控件中搜索与指定字符串完全匹配的项。The following code example demonstrates how to use the ListBox.FindStringExact method to search a ListBox control for an item that exactly matches a specified string. 如果找不到与搜索字符串匹配的项,FindStringExact 将返回-1 值,该示例将显示一个 MessageBoxIf no items are found that match the search string, FindStringExact returns a -1 value and the example displays a MessageBox. 如果找到与搜索文本匹配的项,则该示例使用 SetSelected 方法选择 ListBox中的项。If an item is found that matches the search text, the example uses the SetSelected method to select the item in the 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

注解

此方法执行的搜索不区分大小写。The search performed by this method is not case-sensitive. 搜索将查找与搜索字符串参数中指定的单词完全匹配的内容,sThe search looks for an exact match to the words specified in the search string parameter, s. 您可以使用此方法搜索与指定字符串匹配的第一项。You can use this method to search for the first item that matches the specified string. 然后,可以使用 Remove 方法或更改项的文本,执行删除包含搜索文本的项等任务。You can then perform tasks such as removing the item that contains the search text by using the Remove method or changing the item's text. 找到指定的文本后,如果要在 ListBox中搜索文本的其他实例,则可以使用提供参数的 FindStringExact 方法的版本,该版本用于指定 ListBox内的起始索引。Once you have found the specified text, if you want to search for other instances of the text in the ListBox, you can use the version of the FindStringExact method that provides a parameter for specifying a starting index within the ListBox. 如果要执行部分单词搜索而不是完全匹配的单词,请使用 FindString 方法。If you want to perform partial word search instead of an exact word match, use the FindString method.

另请参阅

FindStringExact(String, Int32)

查找 ListBox 中第一个精确匹配指定字符串的项。Finds the first item in the ListBox that exactly matches the specified string. 搜索从特定的起始索引处开始。The search starts at a specific starting index.

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

参数

s
String

要搜索的文本。The text to search for.

startIndex
Int32

项的从零开始的索引,该项在要搜索的第一个项之前。The zero-based index of the item before the first item to be searched. 设置为负一 (-1) 以从控件的开始处进行搜索。Set to negative one (-1) to search from the beginning of the control.

返回

找到的第一个项的从零开始的索引;如果找不到匹配项,则返回 ListBox.NoMatchesThe zero-based index of the first item found; returns ListBox.NoMatches if no match is found.

异常

startIndex 参数或者小于零,或者大于或等于 Count 类的 ListBox.ObjectCollection 属性的值。The startIndex parameter is less than zero or greater than or equal to the value of the Count property of the ListBox.ObjectCollection class.

示例

下面的代码示例演示如何使用 FindStringExact 方法搜索与指定的搜索文本完全匹配的 ListBox 中的所有项。The following code example demonstrates how to use the FindStringExact method to search for all items in a ListBox that exactly match the specified search text. 该示例使用 FindStringExact 方法的版本,通过该方法可以指定起始搜索索引,以便对 ListBox中的所有项进行持续搜索。The example uses the version of the FindStringExact method that enables you to specify a starting search index from which to do a continual search of all items in the ListBox. 该示例还演示了如何确定在 FindStringExact 方法到达项列表的底部后从列表顶部开始搜索,以防止递归搜索。The example also demonstrates how to determine when the FindStringExact method begins searching from the top of the list after it reaches the bottom of the list of items, to prevent a recursive search. ListBox中找到项后,将使用 SetSelected 方法选择项。Once items are found in the ListBox, they are selected using the SetSelected method.

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

注解

此方法执行的搜索不区分大小写。The search performed by this method is not case-sensitive. 搜索将查找与指定的搜索字符串参数完全匹配的单词,sThe search looks for words that exactly match the specified search string parameter, s. 您可以使用此方法在 ListBox的项列表中搜索与指定字符串匹配的第一项。You can use this method to search for the first item that matches the specified string at the specified starting index within the list of items for the ListBox. 然后,您可以使用 Remove 方法或更改项的文本,执行删除包含搜索文本的项等任务。You can then perform tasks such as removing the item that contains the search text using the Remove method or change the item's text. 此方法通常在使用未指定起始索引的此方法的版本进行调用之后使用。This method is typically used after a call has been made using the version of this method that does not specify a starting index. 在列表中找到了初始项后,此方法通常用于通过在搜索文本的第一个找到实例之后指定该项的 startIndex 参数中的索引位置来查找更多的搜索文本实例。Once an initial item has been found in the list, this method is typically used to find further instances of the search text by specifying the index position in the startIndex parameter of the item after the first found instance of the search text. 如果要执行部分单词搜索而不是完全匹配的单词,请使用 FindString 方法。If you want to perform a partial word search instead of an exact word match, use the FindString method.

备注

当搜索到达 ListBox底部时,它将继续从 ListBox 顶部搜索 startIndex 参数指定的项。When the search reaches the bottom of the ListBox, it continues searching from the top of the ListBox back to the item specified by the startIndex parameter.

另请参阅

适用于