Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

ComboBox.SelectedIndex Property

Gets or sets the index specifying the currently selected item.

[Visual Basic]
Overrides Public Property SelectedIndex As Integer
[C#]
public override int SelectedIndex {get; set;}
[C++]
public: __property int get_SelectedIndex();
public: __property void set_SelectedIndex(int);
[JScript]
public override function get SelectedIndex() : int;
public override function set SelectedIndex(int);

Property Value

A zero-based index of the currently selected item. A value of negative one (-1) is returned if no item is selected.

Exceptions

Exception Type Condition
ArgumentOutOfRangeException The specified index is less than or equal to -2.

-or-

The specified index is greater than or equal to the number of items in the combo box.

Remarks

This property indicates the zero-based index of the currently selected item in the combo box list. Setting a new index raises the SelectedIndexChanged event.

Note   To deselect the currently selected item, set the SelectedIndex to -1.

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, .NET Compact Framework

See Also

ComboBox Class | ComboBox Members | System.Windows.Forms Namespace

Show:
© 2014 Microsoft. All rights reserved.