Keyboard Shortcuts in WinForms ContextMenuStrip (ContextMenuStripEx)
9 Jun 20212 minutes to read
The menu items can be selected through keyboard operation by specifying the shortcuts via the ShortcutKeys
property of the ContextMenu. The ShowShortcutKeys
property is used for display the shortcut key text in the menu item.
NOTE:
- This feature is not applicable for combobox and textbox.
- By using this keyboard shortcuts we can access the menu items through
Click
event.
The below code snippet shows how shortcut is assigned to the menu item.
this.toolStripMenuItem1.ShowShortcutKeys = true;
this.toolStripMenuItem1.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.N)));
Me.toolStripMenuItem1.ShowShortcutKeys = True
Me.toolStripMenuItem1.ShortcutKeys = (CType((System.Windows.Forms.Keys.Control Or System.Windows.Forms.Keys.N), System.Windows.Forms.Keys))
ShortcutKeyDisplayString: User can also specify custom text in place of keyboard shortcuts region using the ShortcutKeyDisplayString
property.
this.toolStripMenuItem1.ShowShortcutKeys = true;
this.toolStripMenuItem1.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.N)));
this.toolStripMenuItem1.ShortcutKeyDisplayString = "Press Ctrl + N";
Me.toolStripMenuItem1.ShowShortcutKeys = True
Me.toolStripMenuItem1.ShortcutKeys = (CType((System.Windows.Forms.Keys.Control Or System.Windows.Forms.Keys.N), System.Windows.Forms.Keys))
Me.toolStripMenuItem1.ShortcutKeyDisplayString = "Press Ctrl + N"