繁体   English   中英

如何在计算器中覆盖?

[英]How do I overwrite in a calculator?

这是我用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;
}

我想你想要

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

编辑: .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();
    }

因为这行:txtDisplay.Clear(); 在您的方法中BtnPlusClick();

如果您只想在开始提供下一个数字时将其清除-只需选中您的bool标志(plusButtonClicked等)-如果其中一个设置为true,则使txtDisplay.Clear();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM