Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 1.1
.NET Framework
Reference
ComboBox Class
Properties
 SelectedIndex Property

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2003/.NET Framework 1.1

Other versions are also available for the following:
.NET Framework Class Library
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

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker