I had a problem, I need the button to be pressed as soon as I press the specified character on the keyboard.
My code is:

public Form1() { InitializeComponent(); } bool press = false; private void Button_Click(object sender, EventArgs e) { if ((textBox1.Text == "0") || (press)) textBox1.Clear(); press = false; Button b = (Button)sender; textBox1.Text = textBox1.Text + b.Text; } private void button1_KeyPress(object sender, KeyPressEventArgs e) { Char ch0 = '0'; if (e.KeyChar == ch0) { if ((textBox1.Text == "0") || (press)) textBox1.Clear(); press = false; Button b = (Button)sender; textBox1.Text = textBox1.Text + b.Text; } } private void button2_KeyPress(object sender, KeyPressEventArgs e) { Char ch1 = '1'; if (e.KeyChar == ch1) { if ((textBox1.Text == "0") || (press)) textBox1.Clear(); press = false; Button b = (Button)sender; textBox1.Text = textBox1.Text + b.Text; } } 

Pressing the button occurs only after selecting the button, but I need to immediately, I would like to shove somewhere
button.Focus ();
so that when you press a button on the keyboard immediately select the desired button and press it, it would correct the error.
Help if you know how to crank it, or in some other way to make the button press right away.

  • It seems to me that you need to use WinApi. Here is an example: www.stackoverflow.com/questions/562753/… otherwise you will have to make a bike. Correct if I'm wrong. - iluxa1810 September
  • Although, you can try to hang the KeyPress event on the main form, and already in the handler to analyze which button you need to call and call the appropriate event handler. - iluxa1810
  • You do not need to throw the focus. You have two events from the user: a click on a specific loaf and a click on a specific button, during which the same action must be performed. The action is carried out in a separate method, which we call when processing these events. And how to intercept keystrokes at the level of the entire form, read here.stackoverflow.com/q/554844/198316 , there are both answers working, choose which one suits you best - rdorn

0