ComboBox.SelectedIndex Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets the index specifying the currently selected item.
public:
virtual property int SelectedIndex { int get(); void set(int value); };
[System.ComponentModel.Browsable(false)]
public override int SelectedIndex { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectedIndex : int with get, set
Public Overrides Property SelectedIndex As Integer
Property Value
A zero-based index of the currently selected item. A value of negative one (-1) is returned if no item is selected.
- Attributes
Exceptions
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.
Examples
The following code example show how to use the FindString method and SelectedIndex property. The example is part of a complete example in the ComboBox class overview.
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.Text);
comboBox1.SelectedIndex = index;
}
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
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.
SelectedIndex, SelectedValue, and FormattingEnabled are related as follows:
If FormattingEnabled is
false
, SelectedIndex will not be set to -1 when SelectedValue is blank.If FormattingEnabled is
true
, SelectedIndex will be set to -1 when SelectedValue is blank.
Note
To deselect the currently selected item, set the SelectedIndex to -1. You cannot set the SelectedIndex of a ComboBox item to -1 if the item is a data-bound item.