ListBox.FindString 方法

定義

找到第一個以指定字串開頭的項目 ListBox

多載

名稱 Description
FindString(String)

找到第一個以指定字串開頭的項目 ListBox

FindString(String, Int32)

找到第一個以指定字串開頭的項目 ListBox 。 搜尋從特定的起始索引開始。

FindString(String)

來源:
ListBox.cs
來源:
ListBox.cs
來源:
ListBox.cs
來源:
ListBox.cs
來源:
ListBox.cs

找到第一個以指定字串開頭的項目 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

要搜尋的文字。

傳回

第一個找到的項目的零基索引;若未找到匹配,則返回 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.cs
來源:
ListBox.cs
來源:
ListBox.cs
來源:
ListBox.cs
來源:
ListBox.cs

找到第一個以指定字串開頭的項目 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),從控制組開始搜尋。

傳回

第一個找到的項目的零基索引;若未找到匹配,則返回 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 方法。

Note

當搜尋到達 的底部 ListBox時,會從背面頂部 ListBox 繼續搜尋到參數指定的 startIndex 項目。

另請參閱

適用於