Use shortcut keys to trigger button events in winform

Source: Internet
Author: User
Settings of shortcut keys and combination buttons in WinForm

 

Method 1 .. Shortcut Keys with complex code and simple operations

Create a blank form

Each form has three events: KeyDown, KeyPress, KeyUp, KeyDown, and KeyPress. However, KeyDown uses KeyCode to correspond to each key on the keyboard, it corresponds to the Keys enumeration, which is more convenient to use. KeyPress uses KeyChar, which requires asc ii encoding, which is inconvenient. KeyUp is an event that is not frequently used. Therefore, we use the KeyDown event to set the shortcut key of the form. Double-click the blank code generated after the event. First, we set a single button, let it press F11 to maximize, press restore again, and add the following code to the event:

if (e.KeyCode == Keys.F11)
{
    if (WindowState == FormWindowState.Maximized)
        WindowState = FormWindowState.Normal;
    else
        WindowState = FormWindowState.Maximized;
}

If the WindowState of the form is Maximized, restore it. Otherwise, maximize the value. Run the following command to try it. It is easy to use.

Below is a TextBox named txtInput added to the form, two buttons named btnConfirm and btnExit:

Run the form again and press F11. Does it work? What's going on?

After the form is run, the focus is no longer on the form, but on the control, we need to set KeyPreview to True, register the keyboard event generated on the control to the form, and run it again, the problem is solved.

How can I set the combination of buttons? For example, to press Alt and the number 0, you need:

if (e.Modifiers == Keys.Alt && e.KeyCode == Keys.D0)
{
    MessageBox.Show("Alt + 0 pressed");
}

You can use Modifiers to set the key combination. The Keys in the numeric area of the keyboard start with D, and the numbers on the keypad start with NumPad. Press Ctrl + Alt + number 0:

if ((int)e.Modifiers == ((int)Keys.Control + (int)Keys.Alt) && e.KeyCode == Keys.D0)
{
    MessageBox.Show("Control + Alt + 0");
}

Convert the enumerated values of Ctrl and Alt to the int type and compare them with the Modifiers to determine whether the key combination is pressed.

How can we trigger a button event?

For example, 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 allow this shortcut key to trigger the event and add a code

E. Handled = true; // set Handled to true to indicate that the KeyPress event has been processed.
Confirm1.w.mclick (); // execute the action of clicking confirm1
}
}

 
   

In addition, the buttons associated with the AcceptButton attribute of the form correspond to the Enter key on the keyboard, and the buttons associated with the CancelButton attribute of the form, it corresponds to the Ecs key on the keyboard.

Another simple shortcut

When setting the Text attribute of a Button, add the & (and) sign before an English letter. For example, set the Text attribute of btnConfirm to: & Confirm. When the form is run, pressing Alt + C is equivalent to pressing this 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.