Take the 2-minute tour ×

I have a blank toolbar button on my form and I am adding all its menus and menu items at run-time. I need to add a keyboard shortcut to the menus of this toolbar. How can I do this?

share|improve this question

3 Answers 3

up vote 3 down vote accepted

You can use "&" special symbol in menu item text to mark key. Have a look on this simple example: http://www.java2s.com/Code/CSharp/GUI-Windows-Form/Addshortcutkeytoamenuitem.htm


1) If drop down button has a text in it it's enough to set '&' symbol, like for menus to make it drop. So in this specific case "Actions" string assigned to that button at some point in the code, have to become "&Actions".

2) If it's only image drop down (no text visible on the button) unfortunately '&' symbol trick doesn't work. But you can do, for example, something like this. A pseudocode:

protected override void OnKeyDown(KeyEventArgs e)

    if (e.Alt && e.KeyCode == Keys.A)

Hope this helps.

share|improve this answer
I cannot. this is for design time. I am adding them at run time. with a method that does stuff like : tbrDropDownButton.Text = UCMDefinitions.GetCaption(textId); tbrDropDownButton.Click += new System.EventHandler(this.StripClick); toolStrip1.Items.Add(tbrDropDownButton); –  Blake Sep 2 '11 at 17:44
This code should do it though... shouldn't it? MenuItem miFile = mainMenu.MenuItems.Add("&File"); Wherever you're adding the menu item just make sure to add it like that, right? –  EJC Sep 2 '11 at 17:48
@BDotA: I don't see any design stuff in code on that site. Everything is setuped form the code actually. The issue is may be, that you need to inject '&' into string recovered from UCMDefinitions.GetCaption(textId); at right place. –  Tigran Sep 2 '11 at 17:51
but I am not using MenuItem, I am using 'tbrDropDownButton = new ToolStripDropDownButton();' and then assigning it like my previous comment. so it is a ToolStripDropDownButton –  Blake Sep 2 '11 at 17:53
@Tigran: yep I did that but it is not still working. it does not open it yet. is there a way I can do it in ProcessCmdKey by calling the EVENT HANDLER for it? but what event to call? 'tbrDropDownButton.Text = UCMDefinitions.GetCaption(textId); tbrDropDownButton.ToolTipText = UCMDefinitions.GetCaption(textId); tbrDropDownButton.Click += new System.EventHandler(this.StripClick); toolStrip1.Items.Add(tbrDropDownButton);' –  Blake Sep 2 '11 at 17:56

You aren't showing much code here on what you are doing. I'll take a stab at it:

ToolStripMenuItem tsm = new ToolStripMenuItem("&Test Menu");
tsm.ShortcutKeys = ((Keys)((Keys.Control | Keys.T)));
share|improve this answer

In the place of tbrDropDownButton.Text = UCMDefinitions.GetCaption(textId), use tbrDropDownButton.Text = "&" + UCMDefinitions.GetCaption(textId). This will assign the first letter of the menu item as the shortcut key. If there are multiple items with the same shortcut key, the user will have to press Enter after one or more presses of the shortcut key.

share|improve this answer
I retried the approach of injecting "&" into the test of the toolbar, it IS working WHEN this form is NOT an MDI form, But when it is an MDI form it is NOT working. ( this is a setting in the main application, we can either show this form inside the parent form or we can show it out side of the parent form) –  Blake Sep 2 '11 at 18:14
There is an option to merge menus with the parent and MDI forms. The menu you are seeing may not be coming from the form you think it is. –  xpda Sep 2 '11 at 18:34

Your Answer


