devxlogo

Combo Box Event

Combo Box Event

Two problems can arise when a confused user scrolls up or down with the mouse and then makes a selection with the Enter Key. First, the down arrow fires two events: Change and Click. Second, the Enter key moves focus to the next tab stop, while the mouse click doesn’t cause a loss of focus from the combo box. Therefore, if you place your action code in the Change event, an Up or Down arrow will fire it, which you don’t want. On the contrary, if you place your action code only in the Lost Focus event and the user clicks on a selection, focus won’t move from the combo box, and the user is left staring at the selected text in the combo box and wondering why no action occurred.
This solution filters out Click events generated with arrow keys, and forces the control to lose focus. In the Declarations section of the form, enter this code:

 ' Note: Use an Integer flag variable in ' VB3Dim bNoise as Boolean' True denotes a Noise Event which is to ' be ignored

Enter this code in the Form_Load event:

 bNoise = False        Enter this code in the combo box KeyDown event:Private Sub cbTest_KeyDown(KeyCode As _        Integer, Shift As Integer)        ' If the user is browsing with the         ' arrows, ignore the Click Events        If KeyCode = vbKeyDown Or KeyCode _                = vbKeyUp Then bNoise = TrueEnd Sub

Enter this code in the combo box Click event:

 Private Sub cbTest_Click()        If bNoise Then                ' Ignore Noise events                 ' (up or down arrow)                bNoise = False        Else                ' Force loss of focus                SendKeys "{TAB}", True        End IfEnd Sub

Write code that reacts to a new user selection in the combo box LostFocus event. Don’t send a Tab keystroke because focus has already shifted, and the combo box’s behavior is consistent regardless of how the user selects a new value.

See also  5 Tips for Working With an Onsite Interpreter
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist