Click here to Skip to main content
15,671,316 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I have a form with about 10 text boxes . Now I need to allow the user to press the Enter key .

For Example -:

When i enter the detail in first Textbox user need to press " ENTER ". If user press ENTER button cursor will moving Textbox2.Again press "Enter" cursor will going Textbox 3.So I need to make for all textboxes likethis ? How can i ? Can anyone help me ? Thank You....
Updated 24-Feb-23 0:16am
Bernhard Hiller 9-Jul-13 9:55am    
This is a duplicate of - do not post your questions twice!

try this
on every Textbox keydown event you will have to write this code
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
        End If
    End Sub
Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
        If e.KeyCode = Keys.Enter Then
        End If
    End Sub

And so on
Share this answer
Arjun Kerala 9-Jul-13 4:06am    
Helloo Basmeh .I tried Your Code..Now Working Fine..Thank You So Much
Basmeh Awad 9-Jul-13 4:08am    
This is not "normal" behaviour - ENTER conventionally accepts the form input and moves to the next form, and TAB moves between fields on the same form. The problem is that if you subvert this mechanism, your app is different from all the other ones your user is used to - and that causes confusion, either in your app (and swearing at you because what they expect to happen didn't) or in the other app (and swearing at you because what happens in your app just mucked up their work in a different one)

It isn't a good idea - confusing users is rarely helpful.

Is there a really, really good reason why you want to do this?
Share this answer
Tom T 10-Nov-14 13:10pm    
You're actually making an assumption here; in legacy/character based systems ENTER is actually used to move forward, and the reason for doing this is because a legacy user expects it; i.e.: in that scenario you're not confusing the user, but actually are making it more normal; i.e.: what he or she expects. I know you could argue that these users should be taught the 'new' logic, but a bunch of them are old fogeys with fat fingers that don't want to find the tab key...
OriginalGriff 10-Nov-14 14:09pm    
Indeed - but since this is textbox based, it should follow the rules for windows applications, not command line: even if it is replacing a legacy system that was cmd based.
If it doesn't, then the users will get even more confused since this app "works the way the old one did" and nothing else on the computer does...
Kevin Whitefoot 21-Sep-15 1:58am    
I know that I am replying to an old post but I think it is important to try to combat the fetishisation of standards, the 'rule' is there to serve the users not the other way about and if you don't look at the specific use case you will never know if the rule can reasonably be applied.

Have you ever watched a data entry clerk or travel agent use a program? The bulk of the typing is on the numeric keypad and the enter key is duplicated there and is the most natural key to use. Remember that a lot of users of specialized programs use only that program on that computer and can be expected to be trained in its use, also they in turn expect that the keys they have to use will be convenient and the tab key is not convenient for data entry.
You could try to keep a list of Textboxes and focus the one with the next TabIndex when enter is hit.

Something like this might work for you;

Public Class Form1

    Private ReadOnly textBoxes As IList(Of TextBox) = New List(Of TextBox)

    Public Sub New()
        ' This call is required by the designer.

    End Sub

    Private Sub TextBox1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles TextBox1.PreviewKeyDown, TextBox2.PreviewKeyDown, TextBox3.PreviewKeyDown
        If e.KeyCode = Keys.Enter Then
            Dim source As TextBox = CType(sender, TextBox)

            Dim nextTextbox As TextBox = textBoxes.Where(Function(tb) tb.TabIndex > source.TabIndex).OrderBy(Function(tb) tb.TabIndex).FirstOrDefault()
            If nextTextbox Is Nothing Then nextTextbox = textBoxes.First()

        End If
    End Sub
End Class

Hope this helps,
Share this answer
Arjun Kerala 9-Jul-13 4:08am    
Dear Frederik , Thank You So Much......
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            Exit Sub
        End If
        e.SuppressKeyPress = True 'this will prevent ding sound 
    End Sub
Share this answer
CHill60 12-Nov-16 21:40pm    
The question is over 3 years old. You have not really provided a "solution" ... just some fairly poor code. Avoid this please
yogesh vaidya 26-Nov-17 15:53pm    
Thanks, sir ,
I tryed your code and it's works perfectly , but not worked as my instruction,
I want to select next control like private sub or any other works , when I press enter it is worked perfectly but when I pressed tab he move to next text box ,how I include enter end tab as same work , ( )
Private Sub textBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles textBox1.KeyDown
    ' Determine whether the key entered is the F1 key. If it is, display Help.
    If e.KeyCode = Keys.Enter Then
    End If
End Sub 'textBox1_KeyDown
Share this answer
muneebalikiyani 9-Jul-13 5:16am    
Right answer, but @Basmeh answered before
Member 11822221 6-Oct-16 2:08am    
I have a query since I am a novice at this,
I have to type a whole page of text into a text editor.
How shall I go about this?
The page is typed in english in notepad and I want to transfer that text to
another text editor that doesn't allow copy and paste.
So I need to write a program in VB to type the text automatically once,
and stop at the end of the page.
I wrote a program but it types the page over and over again.

Can anyobody suggest hot to write this program.

This is the solution I use for VB.NET

1. Set Keypreview=True in your form properties.

2. Put this code in form keydown event:

If (e.KeyData = Keys.Enter) Then
    'for any multiline control, you have to exit to let multiline                  'textbox intro 'keypressing makes line skips.
    If ActiveControl.Name = txtMyMutilineTextBox.Name Then Exit Sub 
    e.SuppressKeyPress = True
    SelectNextControl(ActiveControl, True, True, True, True)
End If

Enjoy !!!!

Xabier Aberasturi Larruzea
Share this answer
 Private Sub FrmAddUser_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Enter Then
            Me.SelectNextControl(Me.ActiveControl, True, True, False, True)
        End If
    End Sub
Form property must be:
Key preview=true
Share this answer
Richard Deeming 24-Feb-23 6:26am    
So exactly the same as what Solution 6 said, six years ago.

Which was already three years after the question was posted and solved.

Stick to answering new questions unless you actually have something new to add to the discussion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900