ListBox.FindString 方法

定義

尋找 ListBox 中第一個以指定字串為開頭的項目。

多載

FindString(String)

尋找 ListBox 中第一個以指定字串為開頭的項目。

FindString(String, Int32)

尋找 ListBox 中第一個以指定字串為開頭的項目。 搜尋作業將於指定起始索引處開始進行。

FindString(String)

尋找 ListBox 中第一個以指定字串為開頭的項目。

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

參數

s
String

要搜尋的文字。

傳回

Int32

第一個找到項目之以零起始的索引,如果沒有相符的項目則傳回 ListBox.NoMatches

例外狀況

s 參數的值小於 -1,或者大於等於項目計數。

範例

下列程式碼範例示範如何使用 FindString 方法來搜尋 中 ListBox 字串的第一個實例。 如果找不到符合搜尋字串 FindString 的專案會傳回 -1 值,而且範例會顯示 MessageBox 。 如果找到符合搜尋文字的專案,此範例會使用 SetSelected 方法來選取 中的 ListBox 專案。

private:
   void FindMyString( 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->FindString( searchString );

         // Determine if a valid index is returned. Select the item if it is valid.
         if ( index != -1 )
                  listBox1->SetSelected( index, true );
         else
                  MessageBox::Show( "The search string did not match any items in the ListBox" );
      }
   }
private void FindMyString(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.FindString(searchString);
      // Determine if a valid index is returned. Select the item if it is valid.
      if (index != -1)
         listBox1.SetSelected(index,true);
      else
         MessageBox.Show("The search string did not match any items in the ListBox");
   }
}
Private Sub FindMyString(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.FindString(searchString)
      ' Determine if a valid index is returned. Select the item if it is valid.
      If index <> -1 Then
         listBox1.SetSelected(index, True)
      Else
         MessageBox.Show("The search string did not match any items in the ListBox")
      End If
   End If
End Sub

備註

此方法執行的搜尋不會區分大小寫。 搜尋會尋找部分符合指定搜尋字串參數的字組。 s 您可以使用這個方法來搜尋符合指定字串的第一個專案。 然後,您可以使用 方法來移除包含搜尋文字 Remove 的專案,或變更專案的文字等工作。 找到指定的文字之後,如果您想要在 中 ListBox 搜尋其他文字範例,則可以使用 方法版本 FindString ,提供 參數,以在 內 ListBox 指定起始索引。 如果您想要執行搜尋完全字組比對,而不是部分相符專案,請使用 FindStringExact 方法。

另請參閱

適用於

FindString(String, Int32)

尋找 ListBox 中第一個以指定字串為開頭的項目。 搜尋作業將於指定起始索引處開始進行。

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

參數

s
String

要搜尋的文字。

startIndex
Int32

搜尋第一個項目之前,項目以零為起始的索引。 設為負一 (-1) 便可從控制項的開頭開始搜尋。

傳回

Int32

第一個找到項目之以零起始的索引,如果沒有相符的項目則傳回 ListBox.NoMatches

例外狀況

startIndex 參數小於零,或者大於等於 Count 類別的 ListBox.ObjectCollection 屬性值。

範例

下列程式碼範例示範如何使用 FindString 方法來搜尋 專案中的所有 ListBox 搜尋文字範例。 此範例使用 方法的版本 FindString ,可讓您指定開始搜尋索引,以便從中持續搜尋所有專案 ListBox 。 此範例也會示範如何判斷 FindString 方法在到達專案清單底部之後,如何從清單頂端開始搜尋,以防止遞迴搜尋。 在 中找到 ListBox 專案之後,就會使用 SetSelected 方法選取這些專案。

private:
   void FindAllOfMyString( 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->FindString( searchString, x );

            // If no item is found that matches exit.
            if ( x != -1 )
            {
               // Since the FindString 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 FindAllOfMyString(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.FindString(searchString, x);
         // If no item is found that matches exit.
         if (x != -1)
         {
            // Since the FindString 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 FindAllOfMyString(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.FindString(searchString, x)
         ' If no item is found that matches exit.
         If x <> -1 Then
            ' Since the FindString 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 指定索引位置。 如果您想要執行搜尋完全字組比對,而不是部分相符專案,請使用 FindStringExact 方法。

注意

當搜尋到達 的 ListBox 底部時,它會繼續從 頂端 ListBox 搜尋回 參數所 startIndex 指定的專案。

另請參閱

適用於