How do I convert Enter Key to Tab Key

Juan Velasquez
Juan Velasquez used Ask the Experts™
on
I am using the attached code to intercept the enter key so that it works like a tab key. I works find for text, list boxes, and combo boxes.  However when the focus is moved to a button, the enter key reverts to being a enter key.  What event of a button, do I have to hook this code to
Private Sub nametxt_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgvStagingMetricsList.KeyDown, _
    txtMetric.KeyDown, txtSelectedFile.KeyDown, txtTechnology.KeyDown, txtUnits.KeyDown

        If e.KeyCode = Keys.Enter Then
            Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
        End If

    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Private Sub nametxt_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles nametxt.KeyDown, gendertxt.KeyDown

    If e.KeyCode = Keys.Enter Then
         Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
    End If
    End Sub

You can use the same event on all textboxes by adding the control.Keydown event from the Handles keyword of that event.

'This will stop the chime sound when Enter is press.
If e.KeyCode = Keys.Enter Then

e.SuppressKeyPress = True
Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
End If    

Set the KeyPreview attribute on your form to True, then use the KeyPress event at your form level to detect the Enter key.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
I would do it this way instead...  =)
(NO KeyPreview needed)
Public Class Form1

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Select Case keyData
            Case Keys.Enter
                    SendKeys.Send("{Tab}")
                    Return True

        End Select
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

End Class

Open in new window

CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Author

Commented:
Hello Idle Mind,
I understand the code except for the return portion of it. Could you explain where it is being returned to .  I'm new to vb.net and while the code works, I would like to know how that section works.  This way, I'm not cutting and pasting code with no understanding of how it works.  Thanks again.
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
See Form.ProcessCmdKey() Method:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.processcmdkey.aspx

    "Return Value ... true if the keystroke was processed and consumed by the control; otherwise, false to allow further processing."

So by returning true we suppress the default behavior of Enter key.

Note that we are OVERRIDING the default ProcessCmdKey() function of the form with the "Overrides" keyword.  So the built-in and hidden function is where the return value goes to.

If we are not suppressing the keystroke then we instead allow the default behavior to proceed by calling the built-in method that we were overriding via "MyBase.ProcessCmdKey(msg, keyData)".

Author

Commented:
Thanks for the explanation.  That made things clearer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial