C#
我用两个文本框和按钮制作了一个复杂的计算器。我想只使用键盘来操作计算器。我将相同的代码放入textbox1_Keypress
,textbox2_Keypress
和Form_Keypress
.
- 如果焦点在任何文本框中,则代码运行良好,但它会在文本框中写入算术符号,我必须手动擦除它才能输入另一个值。如何让它不显示在文本框中?
- 虽然我也使用了相同的代码,
Form_keypress
但每当我离开文本框时,代码就不起作用。
关于如何随时随地为键盘立即做出表单响应的任何建议,无论焦点在哪里?
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);
}
}
}