locked
How to assign a shortcut key to a ToolStrip Button? RRS feed

  • Question

  • Does anyone know how do I set shortcut keys to buttons on a ToolStrip? I need that when the user press "Ctrl+S" on the keyboard the "Save" button gets pressed. I expected to be able to asign a shortcut key to the button, but there is no shortcut key property!

    Does anyone know why is this?
    Saturday, November 5, 2005 9:21 AM

Answers

  • Maybe this is because in all windows applications, the tool bar is just short cuts for the menu bar, so they assume that each tool bar button as the equivalent command on the menu bar.

    Maybe you could work-around it by creating menu item and set is short key:

    ToolStripMenuItem button = new ToolStripMenuItem();

    button.Size = new Size(0,0);

    button.ShortcutKeys = (Keys)Shortcut.CtrlS;

    add this to a menu, but always make shore that the menu isn't showing.

    Tuesday, January 3, 2006 2:23 PM

All replies

  • I'm looking for the same. Do have found an answer yet??
    Tuesday, January 3, 2006 7:59 AM
  • The best thing I could find is that toolstrip buttons (at least when I copied and pasted the example from .Net 2 SDK Documentation) was that if you insert a & in the Text property of the button you can get a nice Alt+whatever as per normal button accelerator behavior. But for toolstrip arbitrary hotkey I did not detect anything so far.

     

    Tuesday, January 3, 2006 10:36 AM
  • Well, that is an start. But I'm really wondering why can't I just set a shortcut key to a ToolStrip Button. There must be a reason.
    Tuesday, January 3, 2006 1:35 PM
  • Maybe this is because in all windows applications, the tool bar is just short cuts for the menu bar, so they assume that each tool bar button as the equivalent command on the menu bar.

    Maybe you could work-around it by creating menu item and set is short key:

    ToolStripMenuItem button = new ToolStripMenuItem();

    button.Size = new Size(0,0);

    button.ShortcutKeys = (Keys)Shortcut.CtrlS;

    add this to a menu, but always make shore that the menu isn't showing.

    Tuesday, January 3, 2006 2:23 PM
  • I think eligazit is on the right track. I did what he suggested, and it worked for me.

    (Of course I also had to hook up an event handler to the menu item in order to get called when the shortcut is executed.)

     

    Wednesday, March 1, 2006 8:35 AM
  • protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { switch (keyData) { case (Keys.Control | Keys.Alt | Keys.H): { btnHome.PerformClick(); break; } } return base.ProcessCmdKey(ref msg, keyData); } Add this to the class containing the toolstrip control. And enjoy.
    • Proposed as answer by thancock14 Wednesday, August 31, 2011 3:54 PM
    Tuesday, July 14, 2009 11:21 AM
  • Yuvraj A Ajatshatru!!  Touche' !!  I used your override of ProcessCmdKey and it worked perfectly!
    Wednesday, August 31, 2011 3:44 PM
  • works perfectly, thanks much
    Friday, June 22, 2012 3:19 PM
  • Sorry I'm a newbie to vb.net.  I know this is probably really basic.  How do I add this to the class containing the toolstrip control?  Where is it?

    Thanks,

    Stacy

    Friday, August 10, 2012 9:53 PM
  • Sorry I'm a newbie to vb.net.  I know this is probably really basic.  How do I add this to the class containing the toolstrip control?  Where is it?

    Thanks,

    Stacy

    A full explanation is in http://support.microsoft.com/kb/320583 (How to trap keystrokes in .NET controls by using Visual Basic .NET).  There you will be introduced to some other usefull tools.

    Best,

    Eduardo Diaz

    Monday, September 3, 2012 6:29 AM