12

I want to be able to show the hotkey combination assigned to a toolstrip menu item in winforms. For instane, in any program (even your browser settings menu) you can see various menu items, and generally, aligned to the right of their item, is their hotkey shortcut. I want to do this programmatically.

Example: Instead of typing

Open a file                         (ctrl+O)

I want to have the properties show up independently of each other.

How can I achieve this?

jwarner112
  • 1,492
  • 2
  • 14
  • 29
  • 5
    I believe you want the [ToolStripMenuItem.ShortcutKeyDisplayString](http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripmenuitem.shortcutkeydisplaystring.aspx) property. – Trevor Elliott Oct 11 '13 at 15:42
  • @TrevorElliott Thanks! This was what I needed, if you put this as an answer I can vote it as the answer? – jwarner112 Oct 14 '13 at 12:01

1 Answers1

14

You want to you use the ShortcutKeys property of the ToolStripMenuItem. This will let you pick the particular key combination you want for each menu item and it will show up to the right of the menu item. Make sure that you have ShowShortcutKeys property of the ToolStripMenuItem set to true.

Scott Wylie
  • 4,725
  • 2
  • 36
  • 48
  • 5
    It appears that ShortcutKeyDisplayString was the property I needed to use. – jwarner112 Oct 14 '13 at 12:00
  • The ShortcutKeyDisplayString only sets the text. The ShortcutKeys property sets the actual short cut key for the user to use. The DisplayString is just for show so if you don't use ShortcutKeys the user's action to what you show in DisplayString will not work. – Scott Wylie Oct 14 '13 at 18:21
  • Yes, though I've already handled the actual shortcut interpretation, I just needed the text. No matter, you're getting Best Answer! – jwarner112 Oct 15 '13 at 13:22
  • 1
    To add, I was trying to set shortcut keys on a `ToolStripMenuItem` that has children, however this did not work. Removing the child items worked. – Slate May 16 '18 at 13:39