VB3 Making ENTER Key Move Focus Like TAB Key for VB Controls

Last reviewed: January 9, 1997
Article ID: Q85562
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

SUMMARY

You can cause the ENTER key to move the focus to the control with the next higher TabIndex property value, as the TAB key does.

However, using the Enter key to move the focus does not follow recommended Windows-based application design guidelines. The Enter key should be used to process the default command or to process entered information, not to move the focus.

MORE INFORMATION

You can detect when the user presses ENTER from the KeyPress event procedure by checking to see if the KeyAscii parameter is the character code for ENTER (13). Then you can move the focus to the next control in the TabIndex order with SendKeys "{tab}". You can move the focus to the previous control with SendKeys "+{tab}".

This technique works with most kinds of controls. It does not work with command button controls, because command buttons do not receive the KeyPress event when you press ENTER.

Steps to Create Example Program

  1. Start Visual Basic for Windows, or from the File menu, choose New Project (press ALT, F, N) if Visual Basic for Windows is already running. Form1 is created by default.

  2. Add two text boxes (Text1 and Text2) to Form1.

  3. Enter the following code in the Text1 KeyPress procedure:

       Sub Text1_KeyPress (KeyAscii As Integer)
          If KeyAscii = 13 Then  ' The ENTER key.
             SendKeys "{tab}"   ' Set focus to next control.
             KeyAscii = 0       ' Ignore this key.
          End If
       End Sub
    
    

  4. Enter the following code in the Text2 KeyPress procedure:

       Sub Text2_KeyPress (KeyAscii As Integer)
          If KeyAscii = 13 Then  ' The ENTER key.
             SendKeys "{tab}"   ' Set focus to next control.
             KeyAscii = 0       ' Ignore this key.
          End If
       End Sub
    
    

  5. Press the F5 key to run the program. When you press ENTER, the focus moves between the two controls.


Additional reference words: 1.00 2.00 3.00 return key
KBCategory: kbprg kbcode
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: January 9, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.