Microsoft KB Archive/99875

= If Invoked by Access Key, Click Event Handled Before LostFocus =

Article ID: 99875

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition
 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic for MS-DOS

-



This article was previously published under Q99875



SUMMARY
Below is an example showing that the Click and LostFocus events occur in different order depending upon whether you cause the click event with the mouse or the keyboard (with an access key). This behavior is by design.

When the focus changes between controls, the Click event can occur before the LostFocus event in some situations. This is mainly because certain events (including GotFocus, LostFocus, and clicking the button with the mouse) are posted to a message queue and other events, such as ALT+V from the keyboard, are issued directly.

To force the code for the LostFocus event to always execute before the Click event code, place a DoEvents statement at the beginning of the Click event code.



Steps to Reproduce Behavior

 * 1) Draw a text box (Text1) and a command button (Command1) on the default Form1.
 * 2) Set the Caption property of Command1 to &Valid. The &V sets up the ALT+V as a way to execute the Command1 button from the keyboard.
 * 3) Add a Beep statement to the Text1_LostFocus event procedure.
 * 4) Add an End statement to the Command1_Click event procedure.
 * 5) Press F5 to run the program. The focus starts by default on the Text1 box. Click the Command1 button, and notice that the LostFocus event occurs and you hear a Beep before the program ends.
 * 6) Press F5 to run the program again. The focus starts by default on the Text1 box. Type ALT+V to activate the Command1 button. Notice that the program ends with no LostFocus event (no beep).

The difference in behavior is not a bug. It is by design.

In order make the Text1_LostFocus event occur first, place a DoEvents statement (or function) at the beginning of the Click event code for the Command1 button.

Additional query words: 2.00 3.00 B_VBMSDOS

Keywords: KB99875

-

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

© Microsoft Corporation. All rights reserved.