ListBox.FindString メソッド

定義

指定した文字列で始まる ListBox の最初の項目を検索します。

オーバーロード

名前 説明
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

検索するテキストを指定します。

返品

最初に見つかった項目の 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 メソッドを使用して検索テキストを含むアイテムを削除したり、アイテムのテキストを変更したりするなどのタスクを実行できます。 指定したテキストが見つかったら、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

検索する最初の項目の前にある項目の 0 から始まるインデックス。 コントロールの先頭から検索するには、負の 1 (-1) に設定します。

返品

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

例外

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

こちらもご覧ください

適用対象