I have a Windows Form shown as a model dialog. It has a context menu of class ContextMenuStrip
. I set shortcuts to several items in the context menu. But this shortcuts works only when context menu is shown. How to make them work even if the context menu is not activated?
The only way I know is to handle KeyPress
event of the form, iterate recursively through all the items in the context menu and compare its ShortcutKeys
property with the actual key pressed. If the match, manually call OnClick
event for this item. Any better ideas?
Use the ToolStripMenuItem.ShortCutKeys property, so that you no need to iterate and call the event handlers.
Sample Code:
ContextMenuStrip _contextMenuStrip = new ContextMenuStrip();
var menuItem = new ToolStripMenuItem("Copy");
menuItem.ShortcutKeys = Keys.Control | Keys.C;
_contextMenuStrip.Items.Add(menuItem);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With