keypress event to make enter key act like tab key

ses41

Newcomer
Joined
Apr 17, 2002
Messages
13
This should be simple, but I can not figure it out.

I have about 100 textbox on a form. The users of this form will be use to having the "enter" key to move on to the next item. The "tab" key performs this function fine, but the users will complain if they do not have the "enter" key.

Can anyone help me with simple code that would be independent of the textbox, that when the "enter" key is pressed, it performs the function of the tab key. I have been able to get it to work for an individual textbox, but I don't want to repeat the code 100 times.
 
First you need to set the form's KeyPreview property to True. Then you can easily catch all keys being pressed in the OnKeyDown event and evaluate them.

Here's the code that changes the focus to the next control when the user hits Enter from within a TextBox;

Visual Basic:
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
    Dim ctl As Control = Me.ActiveControl

    If TypeOf ctl Is TextBox And e.KeyValue = Keys.Enter Then
        Me.SelectNextControl(ctl, True, True, True, True)
        e.Handled = True
    End If
End Sub
 
Thanks so much for the help. I was on the right track, but just couldn't get it done. .NET is a lot different than VB6.
 
I never even knew of that method, wyrd. Just goes to show you learn something new every day.
 
Back
Top