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 與指定字串完全相符的專案控制項。 如果找不到符合搜尋字串的專案,則會傳回 -1 值, FindStringExact 而範例會顯示 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 指定的專案。

另請參閱

適用於