How can I add a shortcut to a certain button in windows forms application?
For an instance I want to assign the combination CTRL+R to my exit button, so when I press it, the code in that particular button executes.
How can I add a shortcut to a certain button in windows forms application?
For an instance I want to assign the combination CTRL+R to my exit button, so when I press it, the code in that particular button executes.
Write an ampersand ("&
") in the Text
property of the button before a character to have Alt+that key as the shortcut.
E.g.:
myButton.Text = "Press &me";
would make Alt+M the shortcut.
Beware of duplicates and ensure you only assign a shortcut once per form. You can also assign shortcuts to e.g. Label
control to make the control next to the label (in the tab order) get the focus, e.g. a text box control.
If you want to have Ctrl instead of Alt take a look at this SO question, as Plue commented. Be aware that Alt+some key is the usual way, so better have a good reason to change the default behavior.