When programming in C #, sometimes you want the control focus to automatically jump from one control to the next control by pressing ENTER. The following is an example of the login interface to explain the two implementation methods.
Problem Description:
Take the login interface as an example, when the user name is entered, to enter the password, the password corresponding to the textbox must get the focus, the general way is to click on the mouse can be. However, the user experience will be poor (because it is necessary to operate the mouse, but also to operate the keyboard), in fact, can be achieved by hitting the key can automatically get the next control focus, so directly with the keyboard input can be, to avoid the mouse operation.
Workaround one: Determine the key, manually jump to the specified method control
private void Textbox1_keydown (object sender, KeyEventArgs e)
{
if (E.keycode = = keys.enter)//if (E.keyvalue = = 13) Judgment is the ENTER key
{
This.textBox2.Focus ();
}
}
Workaround two: Jump according to the control TabIndex property order
private void Textbox1_keydown (object sender, KeyEventArgs e)
{
if (E.keycode = = Keys.enter)
{
This. Selectnextcontrol (this. ActiveControl, True, True, true, true); You need to set the TabIndex order property of the TextBox
}
}
The same way, after the input is completed, you can also press ENTER to log in directly
private void Textbox2_keydown (object sender, KeyEventArgs e)
{
if (E.keyvalue = = 13)
{
This.button1.Focus ();
Button1_Click (sender, E); Invoke event-handling code for the login button
}
}
C # Implement Enter login