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:

  1. This feature is not applicable for combobox and textbox.
  2. By using this keyboard shortcuts we can access the menu items through Click event.
  3. To learn more about the list of Keys Enum, go to ‘link’.

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))

ShortcutKeys

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"

ShortcutKeyDisplayString