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