Getting the next control on the form

Solitaire

Well-known member
Joined
Jun 28, 2004
Messages
465
Location
New York
Programming Experience
10+
Suppose I have several textboxes on a form. When user presses Enter, I want the focus to go to the next textbox control on the form. I forgot the exact syntax for this command. It was a one-line instruction using a method that would simply get the next control on the form, in z-order. I had used it in one of my older programs but just can't find it now.

VB.NET:
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox2.KeyPress, TextBox3.KeyPress, TextBox4.KeyPress
         If e.KeyChar = Chr(13) Then 'focus on next control
 
I found the solution. It involves getting the sender first and then using the GetNextControl method:

VB.NET:
    Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox2.KeyPress, TextBox3.KeyPress, TextBox4.KeyPress
        Dim txt As TextBox = CType(sender, TextBox)
        If e.KeyChar = Chr(13) Then GetNextControl(txt, True).Focus()
    End Sub


Thank you for your assistance.
 
Back
Top