How to implement shortcut keys on toolstripbutton


Author
Message
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
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!
Trent Taylor
Trent Taylor
StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
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


Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
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?
Trent Taylor
Trent Taylor
StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
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.

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
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)?

Trent Taylor
Trent Taylor
StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
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
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Ahhhhh...the light bulb went off. I get it. Thanks, that does make sense!
Trent Taylor
Trent Taylor
StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Cool Smile
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search