hi.
I am new to C #. I want to know is there any way to add shortcuts keys to our application buttons.
I mean if the user press 'n' key on his/her keyboard button1 will triggered it's self and perform the task behind the button.


Plz help..

//designer.cs

this.button.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.button_KeyPress);

from.cs-
  private void button_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar()=='n')
{//ur code}
{}
        }

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

Hi

you can also use key preview of form to catch key code when any control is active. Enable Previewkeydown property and implement:

private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {

        }

Hi

you can also use key preview of form to catch key code when any control is active. Enable Previewkeydown property and implement:

private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {

        }

thanks,it is a great way, is there any way that i can use it in webfrom.aspx?

thanks,it is a great way, is there any way that i can use it in webfrom.aspx?

you have to enable keyPreview to True and on the keydown event of the form you should write that on what keycode it should raise this event.

ex...


if (e.KeyCode == Keys.F8)
{
write the code...
}

Also, if you include an '&' in the buttons text property, the following letter will become an accelerator and respond to Alt+Key combinations.
eg; if you create a button with the text property set to "Re&Bind Data", the buttons text will appear as "ReBind Data" and if you press Alt + B it will perform a button click.

This article has been dead for over six months. Start a new discussion instead.