JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Responding to Combo Box Selections

So you've installed a new combo box in your program, SuperDuperTextPro, to let the user select new text font sizes, and the combo box is staring at you, just a blank box. How do you connect it to your code?

Combo boxes are combinations of text boxes and list boxes, and that combination means that there are two sets of input events: TextChanged events when the user types into the text box, and SelectedIndexChanged,Click, or DoubleClick when the user uses the list box part of the combo box. Note that, unlike standard list boxes, you cannot make multiple selections in a combo box's list box.

Main Page

TextChanged Events

When the user changes the text in a combo box, a TextChanged event occurs, just as it does when the user types in a standard text box. You can read the new text in the text box with the Text property; for example, here's how we display the new text in the combo box every time the user changes that text by typing:

Private Sub ComboBox1_TextChanged(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles ComboBox1.TextChanged
    TextBox1.Text = ComboBox1.Text
End Sub

Main Page

SelectedIndexChanged Events

When the selection changes in a combo box, a SelectionChanged event happens, and you can use the SelectedIndex and SelectedItem properties to get the index of the newly selected item and the item itself. Here's some code from the ComboBoxes example on the CD-ROM that reports the new selection when the user makes a new selection in the combo box:

Private Sub ComboBox1_SelectedIndexChanged(ByVal _
    sender As System.Object, ByVal e As System.EventArgs) _
    Handles ComboBox1.SelectedIndexChanged
    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

You can see the results in Figure 7.13, where the code is responding to a SelectedIndexChanged event (see also "Getting the Current Selection in a Combo Box" in this chapter).

Figure 7.13: Responding to SelectedIndexChanged events in a combo box.

Main Page

Click Events

You also can get Click events when the user makes a selection in the list box using the mouse. You can determine which item the user clicked using the combo's SelectedIndex property, which holds the index of the clicked item, or get that item directly using the SelectedItem property, because when you click an item, it is made the new selected item in the text box.

Main Page

DoubleClick Events

You might expect that where there are Click events there are DoubleClick events, and that's true—but for simple combo boxes only (DropDownStyle = ComboBoxStyle.Simple). When you click an item in the list part of a combo box once, the list closes, so it's impossible to double-click an item—except in simple combo boxes, where the list stays open at all times.

Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor