Click here to Skip to main content
12,896,175 members (51,350 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 21 Aug 2011

Implementing shortcut keys for toolstrip

, 21 Aug 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to activate a toolstrip button via a shortcut key
The first time I used the toolstrip, I was surprised by the fact that toolstrip buttons don't have a shortcut key attribute. Luckily there is a simple workaround.

First, create the button handlers for the toolstrip. E.g.

    private void btnOk_Click(object sender, EventArgs e) {
      Accept = true;
    private void btnCancel_Click(object sender, EventArgs e) {

Next, implement the KeyDown event handler for the form. In the following example, Ctrl+Enter is used to activate the OK button and Escape to activate the Cancel button.

private void RenameF_KeyDown(object sender, KeyEventArgs e) {
  if (e.Control == true && e.KeyValue == 13) {
  if (e.KeyValue == 27) {
     btnCancel_Click(sender, null);

Finally, you must set the form's KeyPreview attribute to true. This has to be done in order to have the keyPress event from the child controls passed to the form's handler.

private void MainF_Load(object sender, EventArgs e) {
  this.KeyPreview = true;

That's it!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Armando de la Torre
Software Developer (Senior) Self-employed
Mexico Mexico
Senior SAP Consultant ( ABAP / MM / Workflow ).
Delphi Developer
C# Asp Developer

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 3 Not bad for custom buttons, but ther... Pin
KenBonny22-Aug-11 21:09
memberKenBonny22-Aug-11 21:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 21 Aug 2011
Article Copyright 2011 by Armando de la Torre
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid