Export (0) Print
Expand All

ComboBox.FindStringExact Method (String, Int32)

Finds the first item after the specified index that matches the specified string.

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

public int FindStringExact (
	string s,
	int startIndex
)
public int FindStringExact (
	String s, 
	int startIndex
)
public function FindStringExact (
	s : String, 
	startIndex : int
) : int

Parameters

s

The String to search for.

startIndex

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.

Return Value

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

Exception typeCondition

ArgumentOutOfRangeException

The startIndex is less than -1.

-or-

The startIndex is equal to the last index in the collection.

The search performed by this method is not case-sensitive. The s parameter is a string to compare against the text associated with the items in the combo box list. The search looks for a 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 partial word search instead of an exact word match, use the FindString method.

The search performed by this method is circular. Although the search starts at the next index after the startIndex parameter, the search restarts at 0 when the end of the collection is reached. However, note that if the startIndex parameter itself is equal to the last index in the collection, an exception is thrown.

The following code example demonstrates how to initialize a ComboBox control by setting the MaxDropDownItems and DropDownStyle properties and using the FindStringExact methods to search the ComboBox. It also shows handling the SelectedIndexChanged event. To run the example, paste the following code in a form that contains a TextBox named TextBox1 and call the InitializeComboBox method from the form's constructor or Load event.

// Declare comboBox1 as a ComboBox.
internal System.Windows.Forms.ComboBox ComboBox1;

// This method initializes the combo box, adding a large string array
// but limiting the drop-down size to six rows so the combo box doesn't 
// cover other controls when it expands.
private void InitializeComboBox()
{
    this.ComboBox1 = new System.Windows.Forms.ComboBox();
    string[] employees = new string[]{"Hamilton, David", "Hensien, Kari",
            "Hammond, Maria", "Harris, Keith", "Henshaw, Jeff D.", 
            "Hanson, Mark", "Harnpadoungsataya, Sariya", 
            "Harrington, Mark", "Harris, Keith", "Hartwig, Doris", 
            "Harui, Roger", "Hassall, Mark", "Hasselberg, Jonas", 
            "Harnpadoungsataya, Sariya", "Henshaw, Jeff D.", 
            "Henshaw, Jeff D.", "Hensien, Kari", "Harris, Keith", 
            "Henshaw, Jeff D.", "Hensien, Kari", "Hasselberg, Jonas",
            "Harrington, Mark", "Hedlund, Magnus", "Hay, Jeff", 
            "Heidepriem, Brandon D."};

    ComboBox1.Items.AddRange(employees);
    this.ComboBox1.Location = new System.Drawing.Point(136, 32);
    this.ComboBox1.MaxDropDownItems = 5;
    this.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
    this.ComboBox1.Name = "ComboBox1";
    this.ComboBox1.Size = new System.Drawing.Size(136, 81);
    this.ComboBox1.TabIndex = 0;
    this.Controls.Add(this.ComboBox1);
    
    // Associate the event-handling method with the 
    // SelectedIndexChanged event.
    this.ComboBox1.SelectedIndexChanged += 
        new System.EventHandler(ComboBox1_SelectedIndexChanged);
}

// Declare comboBox1 as a ComboBox.
System.Windows.Forms.ComboBox comboBox1;

// This method initializes the combo box, adding a large string array
// but limiting the drop-down size to six rows so the combo box doesn't 
// cover other controls when it expands.
private void InitializeComboBox()
{
    this.comboBox1 = new System.Windows.Forms.ComboBox();
    String employees[] = new String[] { "Hamilton, David", "Hensien, Kari", 
        "Hammond, Maria", "Harris, Keith", "Henshaw, Jeff D.", 
        "Hanson, Mark", "Harnpadoungsataya, Sariya", "Harrington, Mark",
        "Harris, Keith", "Hartwig, Doris", "Harui, Roger", "Hassall, Mark",
        "Hasselberg, Jonas", "Harnpadoungsataya, Sariya", 
        "Henshaw, Jeff D.","Henshaw, Jeff D.", "Hensien, Kari", 
        "Harris, Keith", "Henshaw, Jeff D.", "Hensien, Kari", 
        "Hasselberg, Jonas", "Harrington, Mark", "Hedlund, Magnus", 
        "Hay, Jeff", "Heidepriem, Brandon D." };

    comboBox1.get_Items().AddRange(employees);
    this.comboBox1.set_Location(new System.Drawing.Point(136, 32));
    this.comboBox1.set_MaxDropDownItems(5);
    this.comboBox1.set_DropDownStyle(ComboBoxStyle.DropDownList);
    this.comboBox1.set_Name("comboBox1");
    this.comboBox1.set_Size(new System.Drawing.Size(136, 81));
    this.comboBox1.set_TabIndex(0);
    this.get_Controls().Add(this.comboBox1);
    // Associate the event-handling method with the 
    // SelectedIndexChanged event.
    this.comboBox1.add_SelectedIndexChanged(new System.EventHandler(
        comboBox1_SelectedIndexChanged));
} //InitializeComboBox

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft