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

検索するテキストです。

戻り値

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

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

戻り値

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

こちらもご覧ください

適用対象