StrataFrame Forum

How to implement shortcut keys on toolstripbutton

http://forum.strataframe.net/Topic15151.aspx

By Greg McGuffey - 3/24/2008

I can't figure out how to set a keyboard shortcut on a ToolstripButton (I'm assuming that there is an easy way), so the user can use Clt-S to save for example.



So, is there an easy way to do this? Or do I have to do this the hard way (capturing keyboard input, yada, yada)?



Thanks!
By Trent L. Taylor - 3/24/2008

Unfortuately a toolstrip button doesn't have the ShortcutKey that a menustrip item does...however, you can do this pretty easy.  Just override the ProcessCmdKey method and add your logic:

Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
    '-- Check for unique keystrokes
    Select Case keyData
        Case Keys.Control Or Keys.S
            '-- Perform save logic
    End Select

    Return MyBase.ProcessCmdKey(msg, keyData)
End Function

By Greg McGuffey - 3/25/2008

Thanks Trent. I'm assuming this means I'd need to sub-class ToolstripButton, right? If I do that, then I'll need to either manually add buttons via code or manually update the designer file to use my sub-classed button, or is there some other way get a sub-classed toolstrip item into a toolstrip?
By Trent L. Taylor - 3/25/2008

Thanks Trent. I'm assuming this means I'd need to sub-class ToolstripButton, right?

You could, or just add this logic on the form/user control/etc that you want the Ctrl+S to work.  You can do the other and create your own type editor, etc. but that is way more work than it is worth...I would just do the other.

By Greg McGuffey - 3/25/2008

OK, I'm missing something. Blush



I don't know how to override the ProcessCmdKey method on the toolstripbutton outside of subclassing it. How do I do it within the form (for example)?
By Trent L. Taylor - 3/25/2008

Instead of overriding it at the toolstrip or toolstripbutton level, just add the logic in the form or user control that the toolstrip resides in.  This way you only have to override the ProcessCmdKey method on the form versus worrying about this in the toolstrip...this is what I generally do and it makes life a lot easier Smile
By Greg McGuffey - 3/25/2008

Ahhhhh...the light bulb went off. I get it. Thanks, that does make sense!
By Trent L. Taylor - 3/25/2008

Cool Smile