.NET Framework Class Library
ComboBox.SelectedItem Property

Gets or sets currently selected item in the ComboBox.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

Visual Basic (Declaration)
<BindableAttribute(True)> _
Public Property SelectedItem As Object
Visual Basic (Usage)
Dim instance As ComboBox
Dim value As Object

value = instance.SelectedItem

instance.SelectedItem = value
C#
[BindableAttribute(true)] 
public Object SelectedItem { get; set; }
C++
[BindableAttribute(true)] 
public:
property Object^ SelectedItem {
    Object^ get ();
    void set (Object^ value);
}
J#
/** @property */
public Object get_SelectedItem ()

/** @property */
public void set_SelectedItem (Object value)
JScript
public function get SelectedItem () : Object

public function set SelectedItem (value : 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

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.

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++
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() );
}
J#
private void showSelectedButton_Click(Object sender, System.EventArgs e)
{
    int selectedIndex = comboBox1.get_SelectedIndex();
    Object selectedItem = comboBox1.get_SelectedItem();
    MessageBox.Show("Selected Item Text: " + selectedItem.ToString() 
        + "\n" + "Index: " + ((Int32)selectedIndex).ToString());
} //showSelectedButton_Click
Platforms

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

Version Information

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
See Also

Tags :


Page view tracker