Handle the KeyDown event of the TextBox, test for the Enter key and call the Select method of the control you want to shift focus to. If you just want to follow the Tab order then call the SelectNextControl method of the control that currently has focus.
Thanks but how to do the Enter key test?
Private Sub textbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If ??????
e.Handled = True
End If
End Sub
In the KeyPress event of a textbox, the e.KeyChar object is used to capture the key that is pressed. The ASCII value of the Enter key is 13. The Chr() function is used to convert an ASCII value into a character, to be used with the e.KeyChar object.
Enter the following line in the textbox's KeyPress event:
If e.KeyChar = Chr(13) Then txtSecond.Focus()
Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then TextBox2.Focus()
End Sub
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(13) Then TextBox2.Focus()
End Sub
Either of these will work. What is the advantage of one over the other?
VB.NET:Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown If e.KeyCode = Keys.Enter Then TextBox2.Focus() End Sub Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If e.KeyChar = Chr(13) Then TextBox2.Focus() End Sub
Chr(13) is a chryptic "magic number", while Keys.Enter is an easily read (and written) predefined value. You could actually write Chr(Keys.Enter), or Convert.ToChar(Keys.Enter), and those would be better options if you needed that character.Either of these will work. What is the advantage of one over the other?
Chr(13) is a chryptic "magic number", while Keys.Enter is an easily read (and written) predefined value. You could actually write Chr(Keys.Enter), or Convert.ToChar(Keys.Enter), and those would be better options if you needed that character.