How to Make ENTER Key Move Focus Like TAB Key for VB Controls

ID Number: Q85562

1.00

WINDOWS

Summary:

You can cause the ENTER key to move the focus to the control with the

next higher TabIndex property value, just as the TAB key does.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

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}".

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. Run Visual Basic, or from the File menu, choose New Project (ALT,

F, N) if Visual Basic is already running. Form1 will be created by

default.

2. Add two text boxes to Form1 named Text1 and Text2.

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 F5 to run the program. When you press ENTER, the focus moves

between the two controls.

Additional reference words: 1.00 return key