If Invoked by Access Key, Click Event Handled Before LostFocus

ID: Q99875


The information in this article applies to:
  • Microsoft Visual Basic Standard and Professional Editions for Windows, versions 2.0, 3.0
  • Microsoft Visual Basic programming system for Windows, version 1.0
  • Microsoft Visual Basic Standard and Professional Editions for MS-DOS, version 1.0


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.


MORE INFORMATION

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 :
Version :
Platform :
Issue type :


Last Reviewed: September 3, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.