Reputation: 85
I am developing a small application with some buttons and textbox. What I am having problem is assigning a keyboard key (e.g. 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
Upvotes: 0
Views: 5022
Reputation: 314
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);
}
}
Upvotes: 1