Hello :)

Iam using windows application, I want to know how can I move from textbox1 to textbox2 through the tab control key or enter key..

Try this:

Private Sub TextBox1_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown, TextBox2.KeyDown
        If e.KeyCode = Keys.Tab Then
            If sender.Name = "TextBox1" Then
                TextBox2.Focus()
                e.Handled = True
            ElseIf sender.Name = "TextBox2" Then
                TextBox1.Focus()
                e.Handled = True
            End If
        End If
    End Sub

 Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox2.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            If sender.name = "TextBox1" Then
                TextBox2.Focus()
                e.Handled = True
            ElseIf sender.name = "TextBox2" Then
                TextBox1.Focus()
                e.Handled = True
            End If
        End If
    End Sub

1)the tab works by default right?

2)I added the enter code but still when i start my program the focus is always at the 2nd text box, so what can i do?
3) can i move between textboxes using the ASCII code 13? how?
thanks : )

You don't want to do this. The "enter" key is universally used to submit a form, or perform a default action. Altering it is a major violation of the User Model.

sorry but i didn't understand ;/ ,,
what i meant is using enter key to move from one text box to another..
and how I can do it..

I'm very new to programming. . :)

sorry but i didn't understand ;/ ,,
what i meant is using enter key to move from one text box to another..
and how I can do it..

I'm very new to programming. . :)

In the GUI tab there should be an option in the text box to enable or disable the tab key. If it is disabled you should be able to use it to go through different buttons and text fields.

But using enter to do the same would be frowned upon.

now that i have 3 text boxes .. when i type numbers then press enter the numbers will be cleared??
and that doesn't happen with 2nd and 3rd number?

well,
lets say i have 3 textboxes, i entered a value in the 1st box then i press enter, the value will disappear
then the cursor will move to the 2nd box, but the value i type in it doesn't disappear?

Case 13
If sender Is txtA1 Then
txtA2.Focus()
ElseIf sender Is txtA2 Then
txtA3.Focus()
Elseif sender Is txtA3 
txtA1.focus()
End If

I agree with iamthwee! You should not use the enter for simple textbox navigation. Hitting the enter key is like hitting "finished" for having it as anything but submit is a big faux pas. You should set it up with tab. Click on the texbox and open the properties window. Use TabStop to enable or disable it in the tab order. Use "TabIndex" to give it a tab number, 1's the first 2's the second and so on.

I would not use enter if I was you cause if anyone was to use the program they'd fill in the blanks hit enter just to see focus change. They'll be confused.

Rasb

This article has been dead for over six months. Start a new discussion instead.