ListBox.FindStringExact 方法

定义

查找与指定字符串完全匹配的第一项 ListBox

重载

名称 说明
FindStringExact(String)

查找与指定字符串完全匹配的第一项 ListBox

FindStringExact(String, Int32)

查找与指定字符串完全匹配的第一项 ListBox 。 搜索从特定的起始索引开始。

FindStringExact(String)

Source:
ListBox.cs
Source:
ListBox.cs
Source:
ListBox.cs
Source:
ListBox.cs
Source:
ListBox.cs

查找与指定字符串完全匹配的第一项 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

要搜索的文本。

返回

找到的第一项的从零开始的索引;如果未找到匹配项,则 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)

Source:
ListBox.cs
Source:
ListBox.cs
Source:
ListBox.cs
Source:
ListBox.cs
Source:
ListBox.cs

查找与指定字符串完全匹配的第一项 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),从控件的开头进行搜索。

返回

找到的第一项的从零开始的索引;如果未找到匹配项,则 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 项。

另请参阅

适用于