ListBox.FindString Method

Definition

Finds the first item in the ListBox that starts with the specified string.

Overloads

FindString(String)

Finds the first item in the ListBox that starts with the specified string.

FindString(String, Int32)

Finds the first item in the ListBox that starts with the specified string. The search starts at a specific starting index.

FindString(String)

Finds the first item in the ListBox that starts with the specified string.

public:
 int FindString(System::String ^ s);
public int FindString (string s);
member this.FindString : string -> int
Public Function FindString (s As String) As Integer

Parameters

s
String

The text to search for.

Returns

The zero-based index of the first item found; returns ListBox.NoMatches if no match is found.

Exceptions

The value of the s parameter is less than -1 or greater than or equal to the item count.

Examples

The following code example demonstrates how to use the FindString method to search for the first instance of a string in a ListBox. If no items are found that match the search string FindString returns a -1 value and the example displays a MessageBox. If an item is found that matches the search text, the example uses the SetSelected method to select the item in the 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

Remarks

The search performed by this method is not case-sensitive. The search looks for words that partially match the specified search string parameter, s. You can use this method to search for the first item that matches the specified string. You can then perform tasks such as removing the item that contains the search text by using the Remove method or changing the item's text. Once you have found the specified text, if you want to search for other instances of the text in the ListBox, you can use the version of the FindString method that provides a parameter for specifying a starting index within the ListBox. If you want to perform a search for an exact word match instead of a partial match, use the FindStringExact method.

See also

Applies to

FindString(String, Int32)

Finds the first item in the ListBox that starts with the specified string. The search starts at a specific starting index.

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

Parameters

s
String

The text to search for.

startIndex
Int32

The zero-based index of the item before the first item to be searched. Set to negative one (-1) to search from the beginning of the control.

Returns

The zero-based index of the first item found; returns ListBox.NoMatches if no match is found.

Exceptions

The startIndex parameter is less than zero or greater than or equal to the value of the Count property of the ListBox.ObjectCollection class.

Examples

The following code example demonstrates how to use the FindString method to search for all instances of the search text in the items of the ListBox. The example uses the version of the FindString method that enables you to specify a starting search index from which to do a continual search of all items in the ListBox. The example also demonstrates how to determine when the FindString method begins searching from the top of the list after it reaches the bottom of the list of items to prevent a recursive search. Once items are found in the ListBox, they are selected using the SetSelected method.

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

Remarks

The search performed by this method is not case-sensitive. The search looks for words that partially match the specified search string parameter, s. You can use this method to search for the first item that matches the specified string at the specified starting index within the list of items for the ListBox. You can then perform tasks such as removing the item that contains the search text by using the Remove method or changing the item's text. This method is typically used after a call has been made using the version of this method that does not specify a starting index. Once an initial item has been found in the list, this method is typically used to find further instances of the search text by specifying the index position in the startIndex parameter of the item after the first found instance of the search text. If you want to perform a search for an exact word match instead of a partial match, use the FindStringExact method.

Note

When the search reaches the bottom of the ListBox, it continues searching from the top of the ListBox back to the item specified by the startIndex parameter.

See also

Applies to