PRB: Access Key Causes Different Event Order than Mouse Click

Last reviewed: December 11, 1997
Article ID: Q74905
The information in this article applies to:
  • Microsoft Visual Basic Control Creation, Learning, Professional, and Enterprise Editions for Windows, version 5.0
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0, 3.0, 4.0
  • Microsoft Visual Basic programming system for Windows, version 1.0
  • Microsoft Visual Basic for MS-DOS, version 1.0

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.

WORKAROUND

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 "Microsoft Visual Basic: Programmer's Guide" 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 "Command&2."

  4. Run the program.

  5. When Command1 has the focus and you click Command2, the following events are generated in the following order:

        - Command1_LostFocus
        - Command2_GotFocus
        - Command2_Click
    

  6. When Command1 has the focus and you press the access key, ALT+2, the following events are generated in the following order:

        - Command2_Click
        - Command1_LostFocus
        - Command2_GotFocus
    

Sample Code:

   Sub Command1_Click ()
      Print "Command1_click"
   End Sub

   Sub Command1_LostFocus ()
      Print "Command1_lostfocus"
   End Sub

   Sub Command1_GotFocus ()
       Print "Command1_gotfocus"
   End Sub

   Sub Command2_Click ()
       Print "Command2_click"
   End Sub

   Sub Command2_LostFocus ()
      Print "Command2_lostfocus"
   End Sub

   Sub Command2_GotFocus ()
      Print "Command2_gotfocus"
   End Sub

REFERENCES

For additional information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q177992
   TITLE     : HOWTO: Intercept Keyboard Input from Visual Basic


Additional query words: shortcutkey hotkey
Keywords : EnvtRun PrgCtrlsStd VB4ALL VB4WIN vb5all kbfasttip
Version : WINDOWS:1.0,2.0,3.0,4.0,5.0
Platform : MS-DOS WINDOWS
Issue type : kbprb


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: December 11, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.