Keyboard Shortcuts in WinForms ContextMenuStrip (ContextMenuStripEx)
4 Feb 20252 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:
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"