Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help with assigning shortcut keys to menu items. I tried something that doesn't work. I am doing this first time, any help or suggestions appreciated.

I need shortcut keys as below:

File -> Alt, F
File -> Exit -> Alt, F, X
Action -> Alt, A
Action -> First -> Alt, A, F
Action -> Next -> Alt, A, N
Action -> Previous -> Alt, A, V
Action- > Last -> Alt, A, L



C#
fileToolStripMenuItem.ShortcutKeys = Keys.Alt|Keys.F;
           exitToolStripMenuItem.ShortcutKeys = Keys.Alt|(Keys.F|Keys.X);
           actionToolStripMenuItem.ShortcutKeys = Keys.Alt|Keys.A;
           firstToolStripMenuItem.ShortcutKeys = Keys.Alt|(Keys.A|Keys.F);
           nextToolStripMenuItem.ShortcutKeys = Keys.Alt|(Keys.A|Keys.N);
           previousToolStripMenuItem.ShortcutKeys = Keys.Alt|(Keys.A|Keys.V);
           lastToolStripMenuItem.ShortcutKeys = Keys.Alt|(Keys.A|Keys.L);


What I have tried:

I have tried using parameter ShortcutKeys to assign fast keys to menu items
Posted
Updated 5-Oct-16 17:38pm
Comments
Suvendu Shekhar Giri 5-Oct-16 22:38pm    
that thrown any error?
Member 12076824 6-Oct-16 10:31am    
Thank you for response no error, but it doesn't work as intended or it just work with ALT,N instead of ALT,A,N for next menu item.

1 solution

Shortcut keys are for a single keystroke into a menu item. For example , "Alt-X" is a common shortcut to File-Exit. If you are only using the single letter after the menu has dropped down, then you just add a modifier to the text of the menu item.
C#
exitToolStripMenuItem.Text="E&xit";

That also puts an underline under the "x", indicating to the user that is the key to press.
 
Share this answer
 
Comments
Member 12076824 6-Oct-16 10:33am    
Thanks for response. I understand that but since we have some of the same keys for button, and its some third party existing system have similar functionality and I am trying to replicate that.
Midi_Mick 6-Oct-16 11:16am    
In that case, your only other option is to intercept the KeyDown event on your form, after having set the KeyPreview on the form to True. Note that this interprets only one key at a time, so you would have to look at the 2nd key press in the KeyDown event of your menu.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900