Note that there are some explanatory texts on larger screens.

plurals
  1. POReading from the keyboard
    primarykey
    data
    text
    <p>I made a complex calculator in <code>C#</code> with two textboxes and buttons. I would like to operate the calculator using only the keyboard. I put the same code in <code>textbox1_Keypress</code>, <code>textbox2_Keypress</code> and in <code>Form_Keypress</code>. </p> <ol> <li>If the focus was in any of the textbox, the code works well but it writes the arithmetic sign in the textbox and I have to erase it manually to enter another value. How to make it not show in the textbox?</li> <li>Although I used the same code also in <code>Form_keypress</code> but whenever I'm out of the textboxes, the code won't work.</li> </ol> <p>Any suggestions about how to make the form response immediately for the keyboard at anytime and no matter where the focus was?</p> <pre><code>private void textBox1_TextChanged(object sender, EventArgs e) { if (textBox1.Text != "") { double d; if (flag == 1) { Double.TryParse(textBox1.Text, out d); getOperand.Real = d; } else if (flag == 2) { Double.TryParse(textBox1.Text, out d); getOperand.Magnitude = d; } } } private void textBox2_TextChanged(object sender, EventArgs e) { if (textBox2.Text != "") { double d; if (flag == 1) { Double.TryParse(textBox2.Text, out d); getOperand.Imag = d; } else if (flag == 2) { Double.TryParse(textBox2.Text, out d); getOperand.Angle = d; } } } private void textBox2_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == '+') { operand1.Real = getOperand.Real; operand1.Imag = getOperand.Imag; flag1 = 1; } else if (e.KeyChar == '-') { operand1.Real = getOperand.Real; operand1.Imag = getOperand.Imag; flag1 = 2; } else if (e.KeyChar == '*') { operand1.Real = getOperand.Real; operand1.Imag = getOperand.Imag; flag1 = 3; } else if (e.KeyChar == '/') { operand1.Real = getOperand.Real; operand1.Imag = getOperand.Imag; flag1 = 4; } else if (e.KeyChar == '=') { operand2.Real = getOperand.Real; operand2.Imag = getOperand.Imag; switch (flag1) { case 1: operand1 = operand1 + operand2; break; case 2: operand1 = operand1 - operand2; break; case 3: operand1 = operand1 * operand2; break; case 4: operand1 = operand1 / operand2; break; } if (flag == 1) { textBox1.Text = string.Format("{0:F2}", operand1.Real.ToString()); textBox2.Text = string.Format("{0:F2}", operand1.Imag.ToString()); } else if (flag == 2) { textBox1.Text = string.Format("{0:F2}", operand1.Magnitude.ToString()); textBox2.Text = string.Format("{0:F2}", operand1.Angle.ToString()); } // MessageBox.Show(string.Format("{0:D2}", operand1.Real.ToString())); // MessageBox.Show(string.Format("{0:D2}", operand1.Imag.ToString())); listBox1.Items.Add(operand1); } } private void Form1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == '+') { operand1.Real = getOperand.Real; operand1.Imag = getOperand.Imag; flag1 = 1; } else if (e.KeyChar == '-') { operand1.Real = getOperand.Real; operand1.Imag = getOperand.Imag; flag1 = 2; } else if (e.KeyChar == '*') { operand1.Real = getOperand.Real; operand1.Imag = getOperand.Imag; flag1 = 3; } else if (e.KeyChar == '/') { operand1.Real = getOperand.Real; operand1.Imag = getOperand.Imag; flag1 = 4; } else if (e.KeyChar == '=') { operand2.Real = getOperand.Real; operand2.Imag = getOperand.Imag; switch (flag1) { case 1: operand1 = operand1 + operand2; break; case 2: operand1 = operand1 - operand2; break; case 3: operand1 = operand1 * operand2; break; case 4: operand1 = operand1 / operand2; break; } if (flag == 1) { textBox1.Text = string.Format("{0:F2}", operand1.Real.ToString()); textBox2.Text = string.Format("{0:F2}", operand1.Imag.ToString()); } else if (flag == 2) { textBox1.Text = string.Format("{0:F2}", operand1.Magnitude.ToString()); textBox2.Text = string.Format("{0:F2}", operand1.Angle.ToString()); } // MessageBox.Show(string.Format("{0:D2}", operand1.Real.ToString())); // MessageBox.Show(string.Format("{0:D2}", operand1.Imag.ToString())); listBox1.Items.Add(operand1); } } private void textBox2_KeyDown(object sender, KeyEventArgs e) { } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == '+') { operand1.Real = getOperand.Real; operand1.Imag = getOperand.Imag; flag1 = 1; } else if (e.KeyChar == '-') { operand1.Real = getOperand.Real; operand1.Imag = getOperand.Imag; flag1 = 2; } else if (e.KeyChar == '*') { operand1.Real = getOperand.Real; operand1.Imag = getOperand.Imag; flag1 = 3; } else if (e.KeyChar == '/') { operand1.Real = getOperand.Real; operand1.Imag = getOperand.Imag; flag1 = 4; } else if (e.KeyChar == '=') { operand2.Real = getOperand.Real; operand2.Imag = getOperand.Imag; switch (flag1) { case 1: operand1 = operand1 + operand2; break; case 2: operand1 = operand1 - operand2; break; case 3: operand1 = operand1 * operand2; break; case 4: operand1 = operand1 / operand2; break; } if (flag == 1) { textBox1.Text = string.Format("{0:F2}", operand1.Real.ToString()); textBox2.Text = string.Format("{0:F2}", operand1.Imag.ToString()); } else if (flag == 2) { textBox1.Text = string.Format("{0:F2}", operand1.Magnitude.ToString()); textBox2.Text = string.Format("{0:F2}", operand1.Angle.ToString()); } // MessageBox.Show(string.Format("{0:D2}", operand1.Real.ToString())); // MessageBox.Show(string.Format("{0:D2}", operand1.Imag.ToString())); listBox1.Items.Add(operand1); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload