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 メソッドを使用して検索テキストを含むアイテムを削除したり、アイテムのテキストを変更したりするなどのタスクを実行できます。 指定したテキストが見つかったら、ListBox内のテキストの他のインスタンスを検索する場合は、ListBox内で開始インデックスを指定するためのパラメーターを提供する 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 (-1) に設定します。

返品

最初に見つかった項目の 0 から始まるインデックス。は、一致するものが見つからない場合は ListBox.NoMatches を返します。

例外

startIndex パラメーターが 0 未満であるか、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 パラメーターで指定された項目まで検索が続行されます。

こちらもご覧ください

適用対象