首页 文章

如何在计算器中覆盖?

提问于
浏览
0

这是我用SharpDevelop编写的计算器的代码 . 我需要知道如何覆盖一个数字 .

在我的代码中,当我发短信第一个号码时,按下添加按钮后,文本框被清除 .

public partial class MainForm : Form
{
  public MainForm()
  {
    InitializeComponent();
  }

  double total1 = 0;
  double total2 = 0;

  void BtnOneClick(Object sender, EventArgs e)
  {
    txtDisplay.Text = txtDisplay.Text + btnOne.Text;
  }

  void BtnTwoClick(object sender, EventArgs e)
  {
    txtDisplay.Text = txtDisplay.Text + btnTwo.Text;
  }

  ...

  void BtnZeroClick(object sender, EventArgs e)
  {
    txtDisplay.Text = txtDisplay.Text + btnZero.Text;           
  }

  void BtnClearClick(object sender, EventArgs e)
  {
    txtDisplay.Clear();
  }

  void BtnPlusClick(object sender, EventArgs e)
  {
    total1 += double.Parse(txtDisplay.Text);
    txtDisplay.Clear();

    plusButtonClicked = true;
    minusButtonClicked = false;
    multiplyButtonClicked = false;
    divideButtonClicked = false;
  }

  ...

  bool plusButtonClicked = false;
  bool minusButtonClicked = false;
  bool multiplyButtonClicked = false;
  bool divideButtonClicked = false;
}

2 回答

  • 1

    我想你想要的

    void BtnPlusClick(object sender, EventArgs e)
    {
    total1 += double.Parse(txtDisplay.Text);
    txtDisplay.Text = total1.toString();
    

    Edit: .Clear 将从文本框中删除文本..我想如果您想要其他事情发生,那么您需要重新考虑您要做的事情

    Edit2 如果这不是你想要的我不知道是什么

    int total = 0;
        bool newNum = true;
        //1
        private void button1_Click(object sender, EventArgs e)
        {
    
            textBox1.Text = newNum ? "1" : textBox1.Text + "1";
            newNum = false;
        }
        //Add
        private void button2_Click(object sender, EventArgs e)
        {
            newNum = true;
            total += int.Parse(textBox1.Text);
            textBox1.Clear();
        }
        //Equals
        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = total.ToString();
        }
    
  • 0

    这是因为这一行:txtDisplay.Clear();在你的方法BtnPlusClick();

    如果你只想在你开始提供下一个数字时清除它 - 只需chceck你的bool标志(加上按钮等等) - 如果它们被设置为true - 那么make txtDisplay.Clear();

相关问题