我正在制作一个计算器。我希望它也从键盘输入。这是我代码的一部分。问题是,在键盘的第二个输入处+符号没有去,它附加“+”符号,而从按钮点击输入时它不显示+符号。并且告诉我为按钮点击和按键设置功能是正确的还是我应该为每个按钮分开?以及如何在文本框中管理光标?
private void button1_Click(object sender, EventArgs e)
{
yourArithmeticOperation = true; //Enable Arithmetic button click
yourEqual = true; //Enable Equal button click
if (yourNumberControl) //check number button to append or start new
{
if (yourControlClick) //check click button enabled or not
{
textBox1.Text += "1"; //append no.
}
else
return;
}
else
{
textBox1.Text = "1"; //clear textbox and put new no.
yourNumberControl = true; //Enable Number button click
}
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Add)
{
add();
}
}
private void add()
{
yourEqual = false; //Disable equal button
yourNumberControl = true; //enable number button
if (yourArithmeticOperation) //check Arithmetic button
{
num1 = double.Parse(textBox1.Text);
textBox1.Text = "";
equal = "+";
yourArithmeticOperation = false; // disable arithmetic operator
}
else //change the arithmeetic sign
{
textBox1.Text = "";
equal = "+";
}
}