JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Getting the Current Selection in a Combo Box

When you make a selection in a combo box, that new selection appears in the combo box's text box, so it's easy to get the text of the current selection—you just use the combo box's Text property.

You also can use the SelectedIndex and SelectedItem properties to get the index of the selected item and the selected items itself. Here's how I display information about the currently selected item in a combo box in the ComboBoxes example when the user clicks the "Get selected" button:

Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
    Dim selectedIndex As Integer
    selectedIndex = ComboBox1.SelectedIndex
    Dim selectedItem As Object
    selectedItem = ComboBox1.SelectedItem

    TextBox1.Text = "Selected item text: " & selectedItem.ToString() & _
    " Selected index: " & selectedIndex.ToString()
End Sub

If you want to restrict the user's input to items from the combo box's list, set the combo box's DropDownStyle property to DropDownList. In this style of combo boxes, the user cannot type into the text part of the control.

Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor