Click here to Skip to main content
14,640,935 members
Rate this:
Please Sign up or sign in to vote.
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 -:
Textbox1.Text
Textbox2.Text
Textbox3.Text
Textbox4.Text
Textbox5.Text
Textbox6.Text
Textbox7.Text
Textbox8.Text
Textbox9.Text
Textbox10.Text

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....
Posted
Updated 21-Feb-19 23:06pm
v2
Comments
Bernhard Hiller 9-Jul-13 9:55am
   
This is a duplicate of http://www.codeproject.com/Messages/4607116/Re-How-To-Move-TextBoxes-To-Other-Textboxes-By-Pre.aspx - do not post your questions twice!
Rate this:
Please Sign up or sign in to vote.

Solution 2

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
            TextBox2.Focus()
        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
            TextBox3.Focus()
        End If
    End Sub

And so on
   
Comments
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
   
Welcome:-)
Rate this:
Please Sign up or sign in to vote.

Solution 1

Don't.
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?
   
Comments
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.
Rate this:
Please Sign up or sign in to vote.

Solution 3

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.
        InitializeComponent()

        textBoxes.Add(TextBox1)
        textBoxes.Add(TextBox2)
        textBoxes.Add(TextBox3)
    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()

            nextTextbox.Focus()
        End If
    End Sub
End Class


Hope this helps,
Fredrik
   
Comments
Arjun Kerala 9-Jul-13 4:08am
   
Dear Frederik , Thank You So Much......
Rate this:
Please Sign up or sign in to vote.

Solution 5

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            SendKeys.Send("{TAB}")
        Else
            Exit Sub
        End If
        e.SuppressKeyPress = True 'this will prevent ding sound 
    End Sub
   
v2
Comments
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 , (yvmail.acc@gmail.com )
Rate this:
Please Sign up or sign in to vote.

Solution 4

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
        TextBox2.focus
    End If
End Sub 'textBox1_KeyDown
   
Comments
muneebalikiyani 9-Jul-13 5:16am
   
Right answer, but @Basmeh answered before
Member 11822221 6-Oct-16 2:08am
   
Hi
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.

Thanks
Rate this:
Please Sign up or sign in to vote.

Solution 6

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
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100