Partilhar via


Windows Forms – Enable Shortcut Key all over the Form

Windows Application and shortcut key are synonyms. People prefer to use shortcut keys for faster activity. Assume you are in a text box and you want to hit F5 to reload the form. You do not want to move your mouse cursor. So what you will be doing is that you will be adding “Key Down” event to the text box. But if you are in a different text box or in a button that will not work. So you will add “Key Down” event to your form.

But you need to enable a property to work it properly and that’s the trick.

private void Form1_KeyDown(object sender, KeyEventArgs e)

{

    if (e.KeyCode == Keys.F5)

    {

        btnRefresh_Click(null, null);

    }

}

But golden trick is, in the property window of the Form make the following changes

KeyPreview = True

Enjoy programming.

Namoskar!!!

Comments

  • Anonymous
    July 16, 2008
    PingBack from http://blog.a-foton.ru/2008/07/windows-forms-%e2%80%93-enable-shortcut-key-all-over-the-form/

  • Anonymous
    August 01, 2008
    Hi, Great piece of information. It works thanks for the trick Prashant

  • Anonymous
    June 30, 2009
    Thank u, it is the information i was searching for

  • Anonymous
    June 30, 2009
    Thank u, it is the information i was searching for

  • Anonymous
    October 11, 2009
    i tried d same code i also set KeyPreview = True i pressed F5 when d form loaded but it dint work please help!!! thanks in advance

  • Anonymous
    December 10, 2009
    how to put two combition like     (ALT+ S)

    • Anonymous
      July 18, 2016
      void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.Alt && e.KeyCode == Keys.W) { //do something } }
  • Anonymous
    January 26, 2011
    The comment has been removed

  • Anonymous
    March 23, 2011
    Hmm. good but how to do it with mulitple keys....?

  • Anonymous
    August 30, 2012
    Hi, useful code.it works thanks for the trick Bhanu

  • Anonymous
    March 14, 2014
    Thank you very much.  Your post helped me alot.  It works very well.