ComboBox.FindString Method

Definition

Returns the index of the first item in the ComboBox that starts with the specified string.

Overloads

FindString(String)

Returns the index of the first item in the ComboBox that starts with the specified string.

FindString(String, Int32)

Returns the index of the first item in the ComboBox beyond the specified index that contains the specified string. The search is not case sensitive.

FindString(String)

Returns the index of the first item in the ComboBox that starts with the specified string.

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

Parameters

s
String

The String to search for.

Returns

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

Examples

The following code example shows the usage of the FindString method and SelectedIndex property. The example is part of a complete code example in the ComboBox class overview.

void findButton_Click( Object^ sender, System::EventArgs^ e )
{
   int index = comboBox1->FindString( textBox2->Text );
   comboBox1->SelectedIndex = index;
}
private void findButton_Click(object sender, System.EventArgs e) {
    int index = comboBox1.FindString(textBox2.Text);
    comboBox1.SelectedIndex = index;
}
Private Sub findButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim index As Integer
    index = comboBox1.FindString(textBox2.Text)
    comboBox1.SelectedIndex = index
End Sub

Remarks

The search performed by this method is not case-sensitive. The s parameter is a substring to compare against the text associated with the items in the combo box list. The search performs a partial match starting from the beginning of the text, and returning the first item in the list that matches the specified substring. You can then perform tasks, such as removing the item that contains the search text 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 ComboBox, you must use the version of the FindString method that provides a parameter for specifying a starting index within the ComboBox. If you want to perform a search for an exact word match instead of a partial match, use the FindStringExact method.

Applies to

FindString(String, Int32)

Returns the index of the first item in the ComboBox beyond the specified index that contains the specified string. The search is not case sensitive.

public:
 int FindString(System::String ^ s, int startIndex);
public int FindString (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 String to search for.

startIndex
Int32

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

Returns

The zero-based index of the first item found; returns -1 if no match is found, or 0 if the s parameter specifies Empty.

Exceptions

The startIndex is less than -1.

-or-

The startIndex is greater than the last index in the collection.

Remarks

The search performed by this method is not case-sensitive. The s parameter is a substring to compare against the text associated with the items in the combo box list. The search performs a partial match starting from the beginning of the text, returning the first item in the list that matches the specified substring. You can then perform tasks, such as removing the item that contains the search text 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.

Applies to