Make Enter key act like Tab key

B

BobRoyAce

I am using Visual Studio 2005, with VB, and I have a form where I would
like pressing the Enter key to act like pressing the Tab key. In other
words, if user is on a control, and presses Enter, focus would move to
next control in tab order.
 
G

Guest

Try this:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = vbCr Then
SendKeys.Send("{Tab}")
e.Handled = True
End If
End Sub

Hope it helps,

Joris
 
B

BobRoyAce

Thanks Joris...What I'd like to do, though, is specify something at the
form/user control level. Otherwise, I have to remember to use code like
you gave to handle keypresses of ALL controls on the form. Any ideas?

In my old Delphi days, I believe there was like a form-level property
called KeyPreview, and, if it was set to True, the form would check
keypresses before the controls would. Then, there, I could tell the
form to process a CR as a TAB.
 
M

Marius Groenendijk

BobRoy,

This will handle it at the form level for any ctrl on the form
AFAICS KeyPreview is irrelevant

Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
If e.KeyChar = VB.ControlChars.Cr Then
e.Handled = True
Else
MyBase.OnKeyPress(e)
End If
End Sub

HTH,
Marius.
 
M

Marius Groenendijk

Oops, forgot to paste the essential Sub,
the one that does the actual work

Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
If e.KeyCode = Keys.Enter Then
MyBase.ProcessTabKey(Not e.Shift)
e.Handled = True
Else
MyBase.OnKeyDown(e)
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top