แก้ไข

แชร์ผ่าน


ComboBox.SelectedItem Property

Definition

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; }
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.Browsable(false)]
public object? SelectedItem { get; set; }
[<System.ComponentModel.Bindable(true)>]
[<System.ComponentModel.Browsable(false)>]
member this.SelectedItem : obj with get, set
Public Property SelectedItem As Object

Property Value

The object that is the currently selected item or null if there is no currently selected item.

Attributes

Examples

The following code example shows the usage of the SelectedIndex and the SelectedItem properties. 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

Remarks

When you set the SelectedItem property to an object, the ComboBox attempts to make that object the currently selected one in the list. 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. If the object does not exist in the list, the SelectedIndex property is left at its current value.

Note

The ComboBox class searches for the specified object by using the IndexOf method. This method uses the Equals method to determine equality.

Applies to