-2
我製作了一個複雜的計算器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);
}
}
}
請顯示一些相關代碼(即來自'Keypress'處理程序的代碼)。你做過任何研究嗎?你看過['KeyPreview'](http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx)屬性嗎? – CodeCaster
我將KeyPreview設置爲true。 – joseph
我只是將代碼添加到問題中 – joseph