ListBox.FindStringExact 方法

定義

找到第一個與指定字串完全相符的項目 ListBox

多載

名稱 Description
FindStringExact(String)

找到第一個與指定字串完全相符的項目 ListBox

FindStringExact(String, Int32)

找到第一個與指定字串完全相符的項目 ListBox 。 搜尋從特定的起始索引開始。

FindStringExact(String)

來源:
ListBox.cs
來源:
ListBox.cs
來源:
ListBox.cs
來源:
ListBox.cs
來源:
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)

來源:
ListBox.cs
來源:
ListBox.cs
來源:
ListBox.cs
來源:
ListBox.cs
來源:
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 方法。

Note

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

另請參閱

適用於