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?

Dani AI

Generated

For clarity: the thread contains two common scenarios — a Windows Forms app (the VB.Net snippets from and ) and an ASP.NET web page (tagged). The WinForms suggestions in the replies are fine for a simple two-textbox form; the extra notes from (about Alt vs Ctrl) and (marking the key as handled) point to important UX and audible-beep issues.

WinForms notes: prefer handling the KeyDown event for Enter (it covers non-character keys). For data-entry flows that should follow the tab order, trigger a next-control move instead of hard-coding the target; the framework method that advances by TabIndex is documented and handles TabStop settings. To capture keystrokes centrally (all controls) set the form KeyPreview property so the form sees keys before the active control. For multiline boxes, set the control so Enter inserts a newline rather than activating the form default. See the docs for SelectNextControl, Form.KeyPreview and TextBox.AcceptsReturn for details. (learn.microsoft.com)

ASP.NET notes: WebForms can use a Panel or the page/form DefaultButton so Enter triggers a specific button instead of moving focus; that is usually easier than wiring server postbacks for each textbox. For a purely client-side focus move (no postback), capture Enter in JavaScript and call focus() on the next element, for example:

document.getElementById('TextBox1').addEventListener('keydown', function(e) {
  if (e.key === 'Enter' || e.keyCode === 13) {
    e.preventDefault();
    document.getElementById('TextBox2').focus();
  }
});

For Panel/default-button guidance see the Panel.DefaultButton documentation. (learn.microsoft.com)

Recommended Answers

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 meeting, networking, learning, and sharing knowledge.