This documentation is archived and is not being maintained.

ComboBox.FindString Method (String)

.NET Framework 1.1

Finds the first item in the combo box that starts with the specified string.

[Visual Basic]
Overloads Public Function FindString( _
   ByVal s As String _
) As Integer
[C#]
public int FindString(
 string s
);
[C++]
public: int FindString(
 String* s
);
[JScript]
public function FindString(
   s : String
) : int;

Parameters

s
The String to search for.

Return Value

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

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 changeing 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.

Example

[Visual Basic, C#, C++] The following example illustrates the usage of the FindString method and SelectedIndex property. The example is part of a runnable code sample in the ComboBox class overview.

[Visual Basic] 
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

[C#] 
private void findButton_Click(object sender, System.EventArgs e) {
    int index = comboBox1.FindString(textBox2.Text);
    comboBox1.SelectedIndex = index;
}

[C++] 
private:
    void findButton_Click(Object* /*sender*/, System::EventArgs* /*e*/) {
        int index = comboBox1->FindString(textBox2->Text);
        comboBox1->SelectedIndex = index;
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

ComboBox Class | ComboBox Members | System.Windows.Forms Namespace | ComboBox.FindString Overload List

Show: