Export (0) Print
Expand All

ComboBox.SelectedItem Property

Gets or sets currently selected item in the ComboBox.

[Visual Basic]
Public Property SelectedItem As Object
[C#]
public object SelectedItem {get; set;}
[C++]
public: __property Object* get_SelectedItem();
public: __property void set_SelectedItem(Object*);
[JScript]
public function get SelectedItem() : Object;
public function set SelectedItem(Object);

Property Value

The object that is the currently selected item or a null reference (Nothing in Visual Basic) if there is no currently selected item.

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.

Example

[Visual Basic, C#, C++] The following example illustrates the usage of the SelectedIndex and the SelectedItem properties. The example is part of a runnable code sample in the ComboBox class overview.

[Visual Basic] 
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

[C#] 
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());
}

[C++] 
private:
    void showSelectedButton_Click(Object* /*sender*/, System::EventArgs* /*e*/) {
        int selectedIndex = comboBox1->SelectedIndex;
        Object* selectedItem = comboBox1->SelectedItem;

        MessageBox::Show(String::Concat(S"Selected Item Text: ", 
            selectedItem->ToString(), S"\n Index: ", selectedIndex.ToString()));
    }

[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

Show:
© 2014 Microsoft