The setting method of shortcut key and combination key in WinForm _c# tutorial

Source: Internet
Author: User
Tags modifiers
The first method ... Code complex, easy to operate shortcut keys

Create a new blank form


There are 3 events for each form: KeyDown, KeyPress, Keyup,keydown, and KeyPress are pressed to press the event, but KeyDown uses the keycode to correspond to the keys of the keyboard, which is more convenient to use. And KeyPress is using Keychar, this is to find ASC II coding, inconvenient. KeyUp is the key to bounce event, not commonly used, so we use the KeyDown event to set the form's shortcut key, double-click the event after the Blank Generation code, first we set a single button, let it press F11 maximize, again press restore, in the event add the following code:

Copy Code code as follows:

if (E.keycode = = keys.f11)
{
if (WindowState = = formwindowstate.maximized)
WindowState = Formwindowstate.normal;
Else
WindowState = formwindowstate.maximized;
}


If the form of the WindowState is maximized, or to maximize, run the next try, very easy to use:-)

Next, add a textbox named Txtinput to the form, two button named Btnconfirm, Btnexit:



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

Because when the form is running, the focus is no longer on the form, and on the control, we're going to set the KeyPreview to true to register the keyboard events generated on the control with the form to run again, and the problem is resolved.

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

Copy Code code as follows:

if (e.modifiers = = Keys.alt && E.keycode = = keys.d0)
{
MessageBox.Show ("pressed ALT + 0");
}

Use modifiers to set a key combination, the keys of the keyboard number area key are enumerated with D, and the number on the keypad begins with the Numpad. The way you press CTRL and SHIFT keys is similar, let's take a look at the CTRL + ALT + number 0 Setting:

Copy Code code as follows:

if ((int) E.modifiers = = ((int) Keys.control + (int) keys.alt) && E.keycode = = keys.d0)
{
MessageBox.Show ("Press CONTROL + ALT + 0");
}

The CTRL and ALT enumerations are converted to int and then compared to modifiers, which determines whether the key combination is pressed.

So how do you trigger the button event?

Press F1 to trigger Confirm button event

Copy Code code as follows:

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

E.handled = true; Set handled to true to indicate that the KeyPress event has been handled
Confirm1. PerformClick ();////perform a Click Confirm1 action
}
}



In addition, the button associated with the form's AcceptButton property corresponds to the ENTER key on the keyboard, and the button associated with the form's CancelButton property corresponds to the ECS key on the keyboard.

Another way to simply shortcut keys

When you set the Text property of a button, precede an English letter with A & (ampersand), such as setting the Btnconfirm Text property to: &confirm, when you run the form, pressing ALT + C is the equivalent of pressing the key.

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.