Putting hotkey/shortcut text next to toolstrip menu items in winforms
Asked Answered
M

1

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?

Monoatomic answered 11/10, 2013 at 15:39 Comment(2)
I believe you want the ToolStripMenuItem.ShortcutKeyDisplayString property.Pastoralist
@TrevorElliott Thanks! This was what I needed, if you put this as an answer I can vote it as the answer?Monoatomic
S
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.

Sherlock answered 12/10, 2013 at 19:54 Comment(4)
It appears that ShortcutKeyDisplayString was the property I needed to use.Monoatomic
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.Sherlock
Yes, though I've already handled the actual shortcut interpretation, I just needed the text. No matter, you're getting Best Answer!Monoatomic
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.Lynsey

© 2022 - 2024 — McMap. All rights reserved.