简体   繁体   中英

Assign a Keyboard key to a button click WINFORM

I am developing a small application with some buttons and textbox. What I am having problem is assigning a keyboard key (eg F3) to a button click.

For example if the user click the button Cash the code I wanted it's executed fine, but I want to make more easier instead clicking the button with mouse, I want the user be able to press the key on keyboard. I used the keydown event, also keypress event of that button, but still nothing.

I tried this keydown event

          if (e.KeyCode == Keys.Enter)
        {
          btncash.PerformClick()
        }

But still nothing

Do not use F3 function button it's used by OS for activating search. Enter key is fairs click event on focused control so do not use this also. Implement as suggested below. In your Main form

Set KeyPreview to True in form load event. Add KeyDown event handler with the following code

private void Form1_KeyDown(object sender, 
KeyEventArgs e)
{
    if (e.Control && e.KeyCode == Keys.H)
    {
        btncash.PerformClick();
        //btncash_Click(null, null);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM