Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Beginner
Jul 28, 1997

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 
' VB3
Dim 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 = True
End 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 If
End 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.

Les Smith
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date