Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get a clickable button to also activate upon a keypress? (Windows Form C#)

I have a regular button called Btn_Down that activates when clicked, but I also want it to activate when the ‘S’ key is pressed. Can anyone help me out with this?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Subscribe to KeyDown event of form control which contains your button and add following code

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    switch (e.KeyData)
    {
        case Keys.S:
            button1_Click(this, EventArgs.Empty);
            //Or
            //button1.PerformClick();
            break;
    }
}

Set form’s KeyPreview property to true. These settings should give the effect you want.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading