Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set hotkeys for a Windows Forms form

Tags:

I would like to set hotkeys in my Windows Forms form. For example, Ctrl + N for a new form and Ctrl + S for save. How would I do this?

like image 244
Riya Avatar asked Feb 19 '11 03:02

Riya


People also ask

How do I assign a hotkey?

To assign a keyboard shortcut do the following: Begin keyboard shortcuts with CTRL or a function key. Press the TAB key repeatedly until the cursor is in the Press new shortcut key box. Press the combination of keys that you want to assign.

How use function keys for Button in C# Windows form?

Set the button's UseMnemonic property to true and add an ampersand (&) just before the letter in the button's Text property you want to use for the hotkey. The user would press Alt + to activate the hotkey. Show activity on this post. Windows Application and shortcut key are synonyms.


1 Answers

Set

myForm.KeyPreview = true; 

Create a handler for the KeyDown event:

myForm.KeyDown += new KeyEventHandler(Form_KeyDown); 

Example of handler:

    // Hot keys handler     void Form_KeyDown(object sender, KeyEventArgs e)     {         if (e.Control && e.KeyCode == Keys.S)       // Ctrl-S Save         {             // Do what you want here             e.SuppressKeyPress = true;  // Stops other controls on the form receiving event.         }     } 
like image 196
markmnl Avatar answered Sep 20 '22 13:09

markmnl