ListBox.FindStringExact 方法

定义

查找 ListBox 中第一个精确匹配指定字符串的项。

重载

FindStringExact(String)

查找 ListBox 中第一个精确匹配指定字符串的项。

FindStringExact(String, Int32)

查找 ListBox 中第一个精确匹配指定字符串的项。 搜索从特定的起始索引处开始。

FindStringExact(String)

查找 ListBox 中第一个精确匹配指定字符串的项。

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

要搜索的文本。

返回

Int32

找到的第一个项的从零开始的索引;如果找不到匹配项,则返回 ListBox.NoMatches

示例

下面的代码示例演示如何使用 ListBox.FindStringExact 该方法搜索 ListBox 与指定字符串完全匹配的项的控件。 如果未找到与搜索字符串匹配的项目, FindStringExact 则返回 -1 值,该示例显示一个 MessageBox。 如果找到与搜索文本匹配的项,则本示例使用 SetSelected 该方法选择项目中的 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

注解

此方法执行的搜索不区分大小写。 搜索查找与搜索字符串参数 s中指定的单词完全匹配。 可以使用此方法搜索与指定字符串匹配的第一项。 然后,可以通过使用 Remove 方法或更改项目的文本来执行任务,例如删除包含搜索文本的项。 找到指定的文本后,如果要搜索其中 ListBox其他文本实例,则可以使用方法的版本 FindStringExact ,该方法提供参数来指定其中的 ListBox起始索引。 如果要执行部分单词搜索,而不是完全匹配单词,请使用 FindString 该方法。

另请参阅

适用于

FindStringExact(String, Int32)

查找 ListBox 中第一个精确匹配指定字符串的项。 搜索从特定的起始索引处开始。

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

要搜索的文本。

startIndex
Int32

项的从零开始的索引,该项在要搜索的第一个项之前。 设置为负一 (-1) 以从控件的开始处进行搜索。

返回

Int32

找到的第一个项的从零开始的索引;如果找不到匹配项,则返回 ListBox.NoMatches

例外

startIndex 参数或者小于零,或者大于或等于 Count 类的 ListBox.ObjectCollection 属性的值。

示例

下面的代码示例演示如何使用 FindStringExact 该方法搜索与指定搜索文本完全匹配的所有项 ListBox 。 该示例使用方法的版本 FindStringExact ,可用于指定起始搜索索引,以便从中持续搜索所有项 ListBox。 该示例还演示了 FindStringExact 如何在方法到达项列表底部后从列表顶部开始搜索,以防止递归搜索。 在找到 ListBox项后,将使用该方法选择它们 SetSelected

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

注解

此方法执行的搜索不区分大小写。 搜索查找与指定的搜索字符串参数 s完全匹配的单词。 可以使用此方法在项列表中搜索与指定起始索引处的指定字符串匹配的第一个项 ListBox。 然后,可以执行任务,例如使用 Remove 该方法删除包含搜索文本的项或更改项目的文本。 此方法通常在使用未指定起始索引的此方法版本进行调用后使用。 在列表中找到初始项后,此方法通常用于通过在搜索文本的第一个找到实例之后指定项参数中的 startIndex 索引位置来查找搜索文本的更多实例。 如果要执行部分单词搜索,而不是完全匹配单词,请使用 FindString 该方法。

备注

当搜索到达底部 ListBox时,它会继续从背面的顶部 ListBox 搜索到参数指定的 startIndex 项。

另请参阅

适用于