Microsoft KB Archive/74905

= PRB: Access Key Causes Different Event Order than Mouse Click =

Article ID: 74905

Article Last Modified on 8/11/2006

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Control Creation Edition
 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition
 * Microsoft Visual Basic for MS-DOS

-



This article was previously published under Q74905



SYMPTOMS
In Visual Basic, events may be generated in a different order if you choose a control (such as a button, a check box, or an option box) using an access key rather than with the mouse. The events that occur in a different order are Click, LostFocus, and GotFocus.



RESOLUTION
By inserting the DoEvents statement as the very first statement in the Click event handler, you can cause the LostFocus and GotFocus events to be handled before the body of the Click event handler.



STATUS
This behavior is by design. It is not a bug in Visual Basic.



MORE INFORMATION
You can create an access key at design-time by changing the Caption property of a control to include an ampersand (&). The access key is the character after the ampersand, and at run-time you press the ALT+character key to choose the control. (See page 120 of the &quot;Microsoft Visual Basic: Programmer's Guide&quot; version 1.0. manual.)

When you press an access key (ALT+character) to choose a control, the Click event is generated before the LostFocus and GotFocus event; however, when you choose a control by clicking the mouse, the LostFocus and GotFocus events are generated before the Click event.

The example below shows this different order of events. The example uses command buttons, but also applies to Check and Option boxes:


 * 1) Open a new form and create two CommandButtons.
 * 2) Enter the code as shown further below.
 * 3) Change the Caption property of Command2 to &quot;Command&2.&quot;
 * 4) Run the program.
 * 5) When Command1 has the focus and you click Command2, the following events are generated in the following order:


 * 1) * Command1_LostFocus
 * 2) * Command2_GotFocus
 * 3) * Command2_Click
 * 4) When Command1 has the focus and you press the access key, ALT+2, the following events are generated in the following order:


 * 1) * Command2_Click
 * 2) * Command1_LostFocus
 * 3) * Command2_GotFocus

Sample Code:
Sub Command1_Click Print &quot;Command1_click&quot; End Sub

Sub Command1_LostFocus Print &quot;Command1_lostfocus&quot; End Sub

Sub Command1_GotFocus Print &quot;Command1_gotfocus&quot; End Sub

Sub Command2_Click Print &quot;Command2_click&quot; End Sub

Sub Command2_LostFocus Print &quot;Command2_lostfocus&quot; End Sub

Sub Command2_GotFocus Print &quot;Command2_gotfocus&quot; End Sub

Additional query words: shortcutkey hotkey

Keywords: kbprb KB74905

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.