Click here to Skip to main content
15,301,841 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to know that How to focus another textbox after pressing enter.
Means if i am on textbox1 and i press enter then How i can go to textbox2 by pressing enter.?
Posted
Updated 11-Aug-21 6:12am

Here i have tested code u can refer this


VB
Private Sub txtUser_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtUser.KeyDown
    If e.KeyCode = Keys.Enter Then
        txtPass.Focus()
    End If
End Sub



after entering the value to Username textbox as soon the user press ENTER , it will focus to Password textbox.
------

There is a difference between keyPress and keyDown event-
The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events.


http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx[^]

U can also use KeyUp event as SA suggested.

Hope it helps.
   
v2
VB
use Keypress event of TextBox

If e.KeyChar = Chr(13) Then
   TextBox2.Focus
End If
   
v3
Comments
Sergey Alexandrovich Kryukov 16-Jan-13 17:31pm
   
No, it should be KeyDown (or KeyUp), to use Keys.Return... This is really a bad way to do things.
—SA
Member 10331842 26-Oct-13 4:42am
   
how can i use textbox as label with it is fuction example when run a project to display enter your name here when clicked to clear an if clear the text to display default text ?
rb_contra 17-Jan-13 1:04am
   
why i cant use Keypress event...??
   
Because Enter is not a key... (sounds weird, but this is because API terminology is weird). Enter is correctly described as Keys.Return, passed as the parameter to KeyUp or KeyDown, not KeyPress. Look at the event args types and properties of those types.
—SA
rb_contra 6-Nov-13 12:02pm
   
Thanks....
   
You are welcome.
—SA
It's best to check the tabindex value and move to the same textbox with a RETURN that would have happened with a TAB.

Avoid having to write the code for ever specific textbox.
Avoid having the user confused because sometimes TAB would move to a different textbox than RETURN would have.
   

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