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.
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:
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 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.
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.