The setting method of shortcut keys and combination keys in WinForm

Source: Internet
Author: User

Create a new blank form

Each form has such 3 events: KeyDown, KeyPress, Keyup,keydown, and KeyPress are all key press events, but KeyDown is keycode with the keyboard keys, which corresponds to keys enumeration, it is more convenient to use. and KeyPress use is Keychar, this will find ASC II coded, inconvenient. KeyUp is the key to bounce events, not commonly used, so we use the KeyDown event to set the form's shortcut keys, double-click the event after the white space generated code, first we set a single key, let it press F11 to maximize, press the restore again, in the event to add the following code:

Private void Form1_keydown (object  sender, KeyEventArgs e)        {            if (e.keycode = = keys.f11)            {                if (WindowState = = formwindowstate.maximized                )                    {=  Formwindowstate.normal;                }                 Else                 {                    = formwindowstate.maximized;}}        }

If the form of the WindowState is maximized restore, otherwise maximized, run under test, very useful:-)

A TextBox named Txtinput is added to the form below, with two buttons named Btnconfirm, Btnexit:

Run the form again, press F11, it doesn't work? What's going on?

Because the focus is no longer on the form when the form is running, and on the control, we want to set KeyPreview to True to register the keyboard event generated on the control with the form and run it again, the problem is resolved.

Then how to set the combination of buttons? For example, to press ALT and the number 0, you need this:

Private void Form1_keydown (object  sender, KeyEventArgs e)        {            if (e.modifiers = = Keys.alt & & e.keycode = = keys.d0            ) {                MessageBox.Show (" pressed ALT + 0");            }        }

Use modifiers to set keys, keys for keypad number area keys to begin with D, and numbers on Numpad to begin with Numpad. The way you press CTRL and SHIFT keys is similar to the following, let's take a look at how to set the CTRL + ALT + number 0:

Private void Form1_keydown (object  sender, KeyEventArgs e)        {            if (int) E.modifiers = = ( (int) Keys.control + (int) keys.alt) && E.keycode = = keys.d0)            {                MessageBox.Show (  " control + Alt + 0" was pressed);}        }

Converts the enumeration of CTRL and ALT to int to add to the modifiers comparison, so you can determine if the key combination is pressed.

So how do you trigger a button event?

If you press F1 to trigger the Confirm button event

Private void Form1_keydown (object  sender, KeyEventArgs e) {               if (e.keycode = = keys.f1)             {                          // If there is a KeyPress event, do not let this shortcut trigger its event to add a code                       true;   // set handled to true to indicate that the KeyPress event                      has been processed Confirm1. PerformClick (); /// /Perform the action of clicking Confirm1              }}

Additionally, the button associated with the AcceptButton property of the form will correspond to the ENTER key on the keyboard, and the button associated with the form's CancelButton property will correspond to the ECS key on the keyboard.

Another method of simple shortcut keys

When setting the button's Text property, precede an English letter with & (ampersand), such as setting Btnconfirm's Text property to: &confirm, which is equivalent to pressing the key when the form is run and ALT + C is pressed.

The setting method of shortcut keys and combination keys in WinForm

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.