Question leaving textbox

andrews

Well-known member
Joined
Nov 22, 2011
Messages
132
Programming Experience
5-10
My program has a textbox.
When typing something I want to leave the textbox by the enter button and setting the focus and the cursor to another textbox.
How to do?
Thanks for response.
 
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
 
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

Doing as instructed might be a good way to start. The next step might be to read the documentation for the event. They call it the Help menu for a reason.
 
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()
 
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()

Blurgh! Handle KeyDown and compare e.KeyCode or perhaps e.KeyData to Keys.Enter.
 
KeyPress or KeyDown

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
 
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

The first is more correct because you're trying to detect a key, not a character. By the way, as the documentation states, you should call Select rather than Focus to set focus to a control in an application.
 
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.
 
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.

Given that that code is actually testing for a character, it would probably be most appropriate to use ControlChars.Cr, which specifically represents a carriage return. That still doesn't accurately reflect the purpose of the code though. The point of the exercise is to detect the Enter key on the keyboard and the code that does that explicitly is detecting the Enter key in the KeyDown event handler.
 
Back
Top