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

后端 未结 3 665
醉酒成梦
醉酒成梦 2021-01-23 03:13

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 t

3条回答
  •  时光说笑
    2021-01-23 03:36

    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.

提交回复
热议问题