Friends I am new to VB.Net
Please help me
I have two text boxes. When I press the 'Enter' key in the first text box the focus should go to the second textbox
What will be the code?

Recommended Answers

You can use something like this:

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            TextBox2.Focus()
        End If
    End Sub
Jump to Post

I suggest using CTRL instead of ALT for two reasons:

  1. CTRL-ENTER is used in many other apps for this purpose
  2. ALT typically activates the menu
Jump to Post

@ cambalinho
Read the OP's question carefully and before telling me my code is incomplete try it first and put it in the correct event, keydown on textbox1.

Jump to Post

All 9 Replies

easy:

If e.KeyCode = Keys.Return And e.Alt = False Then
            e.SuppressKeyPress = True
            Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
        End If

why i use the Alt key teste?
imagine that you need put a new line in textbox ;)
anotherthing: use the key down instead keypress... or you can't use some keys

You can use something like this:

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            TextBox2.Focus()
        End If
    End Sub

I suggest using CTRL instead of ALT for two reasons:

  1. CTRL-ENTER is used in many other apps for this purpose
  2. ALT typically activates the menu
commented: thansk for correct me ;) +2

Minimalist: i can be mistaked, but i think that you code isn't completed:

 e.SuppressKeyPress = True

i belive these line avoid the enter key continues be pressed.

anotherthing: these line

Me.SelectNextControl(Me.ActiveControl, True, True, True, True)

select the next control by tabindex order.

Reverend Jim: thanks for tell that ;)

@ cambalinho
Read the OP's question carefully and before telling me my code is incomplete try it first and put it in the correct event, keydown on textbox1.

sorry if i was rude... but i thot the:

e.SuppressKeyPress = True

prevent the key down continues

@ cambalinho
Why don't you create a project, 2 textboxes and copy my code into the key_down event in the first textbox. It works perfect as you only catch the enter event and nothing else.

Minimalist: you have right. sorry....
but if you test with:

e.SuppressKeyPress = True

we didn't ear the beep.
sorry if i was rude

You could also use e.handled=true just to add to the fire...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.