ComboBox.SelectedItem 屬性

定義

取得或設定目前在 ComboBox 中選取的項目。Gets or sets currently selected item in the ComboBox.

public:
 property System::Object ^ SelectedItem { System::Object ^ get(); void set(System::Object ^ value); };
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.Browsable(false)]
public object SelectedItem { get; set; }
member this.SelectedItem : obj with get, set
Public Property SelectedItem As Object

屬性值

目前所選取項目的物件;如果目前沒有選取的項目,則為 nullThe object that is the currently selected item or null if there is no currently selected item.

屬性

範例

下列程式碼範例顯示 SelectedIndexSelectedItem 屬性的用法。The following code example shows the usage of the SelectedIndex and the SelectedItem properties. 此範例是 ComboBox 類別總覽中完整程式碼範例的一部分。The example is part of a complete code example in the ComboBox class overview.

void showSelectedButton_Click( Object^ sender, System::EventArgs^ e )
{
   int selectedIndex = comboBox1->SelectedIndex;
   Object^ selectedItem = comboBox1->SelectedItem;
   MessageBox::Show( "Selected Item Text: " + selectedItem->ToString() + "\n" +
      "Index: " + selectedIndex.ToString() );
}
private void showSelectedButton_Click(object sender, System.EventArgs e) {
    int selectedIndex = comboBox1.SelectedIndex;
    Object selectedItem = comboBox1.SelectedItem;

    MessageBox.Show("Selected Item Text: " + selectedItem.ToString() + "\n" +
                    "Index: " + selectedIndex.ToString());
}
Private Sub showSelectedButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim selectedIndex As Integer
    selectedIndex = comboBox1.SelectedIndex
    Dim selectedItem As Object
    selectedItem = comboBox1.SelectedItem

    MessageBox.Show("Selected Item Text: " & selectedItem.ToString() & Microsoft.VisualBasic.Constants.vbCrLf & _
                        "Index: " & selectedIndex.ToString())
End Sub

備註

當您將 SelectedItem 屬性設定為物件時,ComboBox 會嘗試讓該物件成為清單中目前選取的物件。When you set the SelectedItem property to an object, the ComboBox attempts to make that object the currently selected one in the list. 如果在清單中找到物件,它會顯示在 ComboBox 的編輯部分,而 SelectedIndex 屬性會設定為對應的索引。If the object is found in the list, it is displayed in the edit portion of the ComboBox and the SelectedIndex property is set to the corresponding index. 如果物件不存在於清單中,SelectedIndex 屬性會保留在其目前的值。If the object does not exist in the list, the SelectedIndex property is left at its current value.

注意

ComboBox 類別會使用 IndexOf 方法來搜尋指定的物件。The ComboBox class searches for the specified object by using the IndexOf method. 這個方法會使用 Equals 方法來判斷是否相等。This method uses the Equals method to determine equality.

適用於