Export (0) Print
Expand All

ComboBox.FindString Method (String)

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

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public int FindString (
	string s
)
public int FindString (
	String s
)
public function FindString (
	s : String
) : int
Not applicable.

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.

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.

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.

private 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.get_Text());
    comboBox1.set_SelectedIndex(index);
} //findButton_Click

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft