We help IT Professionals succeed at work.

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

ajax2cle
ajax2cle asked
on
379 Views
Last Modified: 2010-05-03
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
Comment
Watch Question

Commented:
try setting "Accepts Enter Key" property = false for all form controls

Commented:
oops, nvm above post
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
Top Expert 2004
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Top Expert 2004

Commented:
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

Commented:
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

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

Commented:
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

Commented:
you said this was a for vb .NET app??? the accepted answer won't work in .NET!!!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.