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

検索するテキストです。

戻り値

最初に見つかった項目の 0 から始まるインデックス番号。一致する項目が見つからない場合は 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 変更したりするなどのタスクを実行できます。 指定したテキストが見つかったら、 内のテキストの他のインスタンスを検索する場合は、 内ListBoxListBox開始インデックスを指定するためのパラメーターを提供する メソッドのFindStringExactバージョンを使用できます。 完全に一致する単語ではなく、部分的な単語検索を実行する場合は、 メソッドを使用します 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

最初の検索対象項目の前にある項目の 0 から始まるインデックス番号。 コントロールの先頭から検索する場合は -1 に設定します。

戻り値

最初に見つかった項目の 0 から始まるインデックス番号。一致する項目が見つからない場合は 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 続行されます。

こちらもご覧ください

適用対象