C#
2 つのテキストボックスとボタンを備えた複雑な電卓を作成しました。キーボードだけで電卓を操作したい。と に同じコードを入れ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);
}
}
}