C # Implement Enter login

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.