ComboBox.SelectedIndex ComboBox.SelectedIndex ComboBox.SelectedIndex ComboBox.SelectedIndex Property

定义

获取或设置指定当前选定项的索引。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; }
member this.SelectedIndex : int with get, set
Public Overrides Property SelectedIndex As Integer

属性值

当前选定项的从零开始的索引。A zero-based index of the currently selected item. 如果未选定任何项,则返回值为负一 (-1)。A value of negative one (-1) is returned if no item is selected.

异常

指定的索引小于或等于 -2。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.

示例

下面的代码示例演示如何使用FindString方法和SelectedIndex属性。The following code example show how to use the FindString method and SelectedIndex property. 该示例是ComboBox类概述中完整示例的一部分。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

注解

此属性指示组合框列表中当前选定项的从零开始的索引。This property indicates the zero-based index of the currently selected item in the combo box list. 设置新索引会引发SelectedIndexChanged事件。Setting a new index raises the SelectedIndexChanged event.

SelectedIndexSelectedValueFormattingEnabled是相关的,如下所示:SelectedIndex, SelectedValue, and FormattingEnabled are related as follows:

备注

若要取消选择当前选定的项, SelectedIndex请将设置为-1。To deselect the currently selected item, set the SelectedIndex to -1. 如果项是数据SelectedIndex绑定项ComboBox ,则不能将该项的设置为-1。You cannot set the SelectedIndex of a ComboBox item to -1 if the item is a data-bound item.

适用于

另请参阅