use Enter button on the keyboard to jump to the next text box

dimeanel

Member
Joined
Apr 12, 2006
Messages
7
Programming Experience
Beginner
I want to use Enter button on the keyboard to jump to the next text box in my form insted of using Tab button during execution the program. Can anyone help me about that?
 
Well there are several ways to achieve the same but i prefer this one:
1st make this procedure:
VB.NET:
Public Sub onEnter_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) 
     If e.KeyCode = Keys.Enter Then 
         SendKeys.Send("{TAB}") 
    End If 
End Sub

and then just add a handler joining this code inside Load event:
VB.NET:
Dim xControl As Control 
For Each xControl In Me.Controls 
     AddHandler xControl.KeyDown, AddressOf onEnter_KeyDown 
Next
Also you could use an arraylist but it's more confusing i'd say

Regards ;)

P.S. pozdrav za stip :)
 
UR: No, "{TAB}" is a string code, not a key in Keys enumeration.
 
I have tried this with the enter key to my code too but there are some bugs with the message boxes .When i use a messagebox to display a message the enter key doesnt work correctly and i have to use the mouse to click the messagebox button.
 
This would be my preference:
VB.NET:
    Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, _
                                                                                                                    TextBox2.KeyPress
        If e.KeyChar = Convert.ToChar(Keys.Enter) Then
            Me.SelectNextControl(DirectCast(sender, Control), True, True, True, True)
        End If
    End Sub
Note that I have cast the sender and passed that to SelectNextControl, which allows you to use this same method to handle events for multiple controls.
 
Back
Top