If Invoked by Access Key, Click Event Handled Before LostFocus

Last reviewed: June 21, 1995
Article ID: Q99875
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0 - Standard and Professional Editions of Microsoft Visual Basic 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.

  • 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.

  • Add a Beep statement to the Text1_LostFocus event procedure.

  • Add an End statement to the Command1_Click event procedure.

  • 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.

  • 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 reference words: 1.00 2.00 3.00 B_VBMSDOS
    KBCategory: kbprg
    KBSubcategory: PrgCtrlsStd


    THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

    Last reviewed: June 21, 1995
    © 1998 Microsoft Corporation. All rights reserved. Terms of Use.