Link to home
Start Free TrialLog in
Avatar of ajax2cle
ajax2cle

asked on

Using the enter key instead of tab in VB.net app

Hi all

I have a feeling this is a pretty stupid question, but the more i look at the problem i'm having, the more at a loss i am for an answer.

In a simple vb application how can I use the <enter> key to tab between fields.

Thanks in advance
Avatar of bobbit31
bobbit31
Flag of United States of America image

try setting "Accepts Enter Key" property = false for all form controls
oops, nvm above post
Avatar of jmwheeler
jmwheeler

I can almost assure you that this is not the best way but if you get stuck this code should work.  It would just get really tedious if you have a lot of controls on your form.

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        Text2.SetFocus
    End If
End Sub


Private Sub Text2_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        Text1.SetFocus
    End If
End Sub
ASKER CERTIFIED SOLUTION
Avatar of Shauli
Shauli

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ps. And if you dont use an array, then you'll have to put the above got in each and every textbox _KeyPress event.
S
Good Point Shauli
for VB .NET (make sure to set the form's "Key Preview" property to true):

    Public Sub Form1_KeyPress(ByVal eventSender As System.Object, ByVal e As KeyPressEventArgs) Handles MyBase.KeyPress
        If e.KeyChar = Chr(13) Then
            MyBase.ProcessTabKey(True)
        End If

    End Sub
On the form where the controls are placed, make the propery of form KeyPreview=True. Then in forms key press event write this

Sorry incomplete post:

On the form where the controls are placed, make the propery of form KeyPreview=True. Then in forms key_press event write this

If KeyAscii = 13 Then
    KeyAscii = 9
End If

When KeyPreview is True on form, all key strokes are received by forms first and then passed to the controls. So if you convert the key stroke to your desired key on form you donot have to put this code with each control on your form.

Ciao
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        KeyAscii=vbKeyTab
    End If
End Sub
I essentially converts the enter keystroke into tab. u have to place this code for all textboxes on the form
you said this was a for vb .NET app??? the accepted answer won't work in .NET!!!