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

검색할 텍스트입니다.

반환

Int32

찾은 첫째 항목의 인덱스(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다른 인스턴스를 검색하려는 경우 내의 시작 인덱ListBox스를 지정하기 위한 매개 변수를 제공하는 메서드의 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로 설정합니다.

반환

Int32

찾은 첫째 항목의 인덱스(0부터 시작)입니다. 일치하는 항목이 없으면 ListBox.NoMatches를 반환합니다.

예외

startIndex 매개 변수가 0보다 작거나 Count 클래스의 ListBox.ObjectCollection 속성 값보다 크거나 같은 경우

예제

다음 코드 예제에서는 메서드를 사용하여 FindString 항목 ListBox에서 검색 텍스트의 모든 인스턴스를 검색하는 방법을 보여 줍니다. 이 예제에서는 모든 항목을 ListBox지속적으로 검색할 시작 검색 인덱스를 지정할 수 있는 메서드의 버전을 FindString 사용합니다. 이 예제에서는 재귀 검색을 방지하기 위해 항목 목록의 맨 아래에 도달한 후 메서드가 목록 맨 위에서 검색을 시작하는 시점을 확인하는 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 계속합니다.

추가 정보

적용 대상