我正在做一个计算器。我希望它也可以从键盘输入。这是我的代码的一部分。问题是,在键盘的第二个输入中,+ 号没有出现,它附加了“+”号,而从按钮单击输入时,它不显示 + 号。并告诉我为按钮单击和按键创建一个功能是否正确,或者我应该为每个功能分开?以及如何管理文本框中的光标?
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 = "+";
}
}