"Go" Add a shortcut key to a control such as a WinForm button

Source: Internet
Author: User

Ref:http://blog.sina.com.cn/s/blog_4cb9953f0100cy4z.html

The first type: ALT + * (Button shortcut key)

When you set the Text property for a button, label, MenuStrip, and so on, add the & key name after the name, for example button1.text= "OK (&o)". There will be shortcut keys, press Alt+o to execute the button click event.

The second type: ctrl+* and other key combinations

In WinForm, set the KeyPreview (Register keyboard event to the form) property to be true for the form to use the key combination.

It then uses the form's KeyDown event (which occurs when a key is first pressed).

Instance code:

private void ***_keydown (object sender, KeyEventArgs e)
{

if (E.keycode = = keys.f && e.control)
{
Button1. PerformClick (); Perform an action that clicks button1
}

}

NOTE: 1, * * * represents the form name, you can look at the "Keys" enumeration parameters to achieve their own needs

2. There is also a problem, when using CTRL + * shortcut keys, when the focus on a writable control (such as a TextBox), the * key value may be entered simultaneously, you need to add another sentence to set handled to True to cancel the KeyPress event.

That

private void ***_keydown (object sender, KeyEventArgs e)
{

if (E.keycode = = keys.f && e.control)
{

E.handled = true; Set handled to true to indicate that the KeyPress event has been processed
Button1. PerformClick ();

}

}

The third type:

Or take the button as an example. Add a contextMenuStrip1 to the form, set it to the button, and assume it is button1. Add an item to CONTEXTMENUSTRIP1, then set the shortcut key for it (which is the shortcut you want to add to the button), and set its Visible property to false. In this way, the shortcut keys for Button1 are set successfully.

"Go" Add a shortcut key to a control such as a WinForm button

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.