Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 3: User Interface Design: Visual Basic Controls and Events
Last change: February 13 2016 18:47:26.
<<PreviousNext>>

SelectedIndexChanged and DoubleClick Events: List Box and Combo Box

The list box and combo box have the SelectedIndexChanged event, which is triggered when the user clicks an item, causing a change in the selected item. When the user selects an item in these two controls, their SelectedIndex and SelectedItem properties are set to the item. You can use this event to retrieve the selection made by the user. For example, you may want to show the current choice in the text box txtName as soon as the user clicks an item in the list box lstName. The event to place the code will be lstName_SelectedIndexCahged, and you will assign the list’s text property to that of the text box. The code will appear as follows:

Private Sub lstName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstName.SelectedIndexChanged ‘Show the item selected in the textbox txtName.Text = lstName.SelectedItem End Sub

Sometimes, you may want to be sure that when the user initiates an action, he means what he is doing; for example, to delete an item when he clicks on it. In such cases, the DoubleClick event would be better than the SelectedIndexChanged event because there is less chance for the user to accidentally double-click on an item. As an illustration, if you want the user to indicate which item to delete from a list box, named lstCandidate, your program can instruct the user to double-click the item to remove. In this case, the event in which to place the code will be lstCandidate_DoubleClick. The item to be removed from the box is indicated by the SelectedIndex property. Remember that when the user clicks an item, the SelectedIndex and SelectedItem properties are set to this item, provided that the SelectionMode is set to One. Note also the SelectedIndexChanged event is always triggered before the DoubleClick event. The code will appear as follows:

Private Sub lstCandidate_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstCandidate.DoubleClick lstCandidate.Items.RemoveAt(lstCandidate.SelectedIndex) End Sub

Combo Box Events

Notice that both the list box and the combo box have the Click event. In the case of the list box, the Click event works pretty much like the SelectedIndexChanged event. However, in the case of the combo box, the Click event may not behave in a manner as one might expect. If the control’s DropDownStyle property is set to either DropDown or DropDownList, the Click event occurs when the user presses the drop-down button to cause the list to drop down, not when he or she makes the selection from the list. The Click event may work as you expected only when its DropDownStyle property is set to Simple. In this case, however, the Click event occurs not only when you click on an item in its list box, but also when you click on its text box. In short, the combo box’s Click event has too many ramifications and usually is not a good choice to handle the user’s click on the control.
The SelectedIndexChanged event is still the event to use in response to the user’s selection of an item in the combo box. Regardless of the DropDownStyle property setting, when this event is triggered literally when the SelectedIndex changes. This is what happens when the user makes a selection in the combo box’s list box.
Notice, however, the SeletedIndexChanged event occurs not just when the user makes a selection, but also whenever there is a change in the SelectedIndex property. The event can be triggered when your code assigns a value to the SelectedIndex property. The following Try This box shows you how this possibility can cause some potential problems.

Try This
Bring a combo box into a new form. Name the combo box cboWeekDays. At design time, set its Items property to contain Sunday, Monday all the way through Saturday. Place the following in the code window:
Private Sub Form1_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Show the first item when the program starts cboWeekDays.SelectedIndex = 0 End Sub Private Sub cboWeekDays_SelectedIndexChanged(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles cboWeekDays.SelectedIndexChanged 'Remove the item selected by the user cboWeekDays.Items.RemoveAt(cboWeekDays.SelectedIndex) End Sub
Run the program, and check the list in the combo box. Do you see Sunday in it? No. It has been removed because when your program starts, the Form Load event assigns 0 to the SelectedIndex property of the combo box, which triggers the SelectedIndexChanged event. In that event procedure, the item pointed to by the current SelectedIndex is removed. All these happen without any selection made by the user. This situation also applies to the list box. Beware!
Last change: February 13 2016 18:47:28.
<<PreviousNext>>