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.