How to open a toolbar menu by Keyboard short-cuts?

寵の児 提交于 2019-12-02 07:15:42

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

EDIT:

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)
    {
        toolStripDropDownButton1.ShowDropDown();
    }
    base.OnKeyDown(e);
}

Hope this helps.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!