在按下相等按钮后让我的计算器取一个新数字

时间:2013-10-31 15:38:28

标签: c# winforms

所以我现在已经有一段时间了,仍然无法让我的计算器做最后的事情。

我明白了!

按下相等按钮或Tan,Sin,Cos或Mod按钮后,我希望我的计算器取一个新数字。基本上好像文本框中没有任何内容,即使答案仍然存在。目前,所有号码都保留,新号码将添加到最后。我不希望这种情况发生。

在计算之后,如果我按下一个数字按钮,我希望它清除屏幕并添加新的数字。下面是我到目前为止的代码。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    Double FirstNumber;
    string mathOperator = "";

    private void AddButton_Click(object sender, EventArgs e)
    {
        FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
        mathOperator = "+";

        DisplayTextBox.Text = "";
        DisplayTextBox.Focus();

    }
    private void EqualButton_Click(object sender, EventArgs e)
    {
        Double SecondNumber;
        SecondNumber = Convert.ToDouble(DisplayTextBox.Text);

        switch (mathOperator)
        {
            case "+":
                DisplayTextBox.Text = (FirstNumber + SecondNumber).ToString();
                break;
            case "-":
                DisplayTextBox.Text = (FirstNumber - SecondNumber).ToString();
                break;
            case "*":
                DisplayTextBox.Text = (FirstNumber * SecondNumber).ToString();
                break;
            case "/":
                DisplayTextBox.Text = (FirstNumber / SecondNumber).ToString();
                break;
            default:
                break;
        }
    }

    private void button12_Click(object sender, EventArgs e)
    {
        FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
        FirstNumber *= -1;
        DisplayTextBox.Text = FirstNumber.ToString();

    }

    private void ButtonMinus_Click(object sender, EventArgs e)
    {

        FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
        mathOperator = "-";

        DisplayTextBox.Text = "";
        DisplayTextBox.Focus();

    }

    private void ButtonMultiply_Click(object sender, EventArgs e)
    {

        FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
        mathOperator = "*";

        DisplayTextBox.Text = "";
        DisplayTextBox.Focus();

    }

    private void ButtonDivide_Click(object sender, EventArgs e)
    {

        FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
        mathOperator = "/";

        DisplayTextBox.Text = "";
        DisplayTextBox.Focus();

    }

    private void ButtonMod_Click(object sender, EventArgs e)
    {
        FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
        DisplayTextBox.Text = Math.Tan(FirstNumber).ToString();

    }

    private void Button1_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = DisplayTextBox.Text + "1";
    }

    private void Button2_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = DisplayTextBox.Text + "2";
    }

    private void Button3_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = DisplayTextBox.Text + "3";
    }

    private void Button4_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = DisplayTextBox.Text + "4";
    }

    private void Button5_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = DisplayTextBox.Text + "5";
    }

    private void Button6_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = DisplayTextBox.Text + "6";
    }

    private void Button7_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = DisplayTextBox.Text + "7";
    }

    private void Button8_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = DisplayTextBox.Text + "8";
    }

    private void Button9_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = DisplayTextBox.Text + "9";
    }

    private void ButtonClear_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = "";
        mathOperator = "";
    }

    private void ButtonOff_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void ButtonTan_Click(object sender, EventArgs e)
    {
        FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
        DisplayTextBox.Text = Math.Tan(FirstNumber).ToString();
    }

    private void ButtonSin_Click(object sender, EventArgs e)
    {
        FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
        DisplayTextBox.Text = Math.Sin(FirstNumber).ToString();

    }

    private void ButtonCos_Click(object sender, EventArgs e)
    {
        FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
        DisplayTextBox.Text = Math.Cos(FirstNumber).ToString();

    }

    private void Button0_Click(object sender, EventArgs e)
    {
        if (DisplayTextBox.Text.Length >= 1)
        {
            DisplayTextBox.Text = DisplayTextBox.Text + "0";
        }
        else
        {
            return;
        }
    }

    private void DecimalButton_Click(object sender, EventArgs e)
    {
        if (DisplayTextBox.Text.Contains("."))
        {
            return;
        }
        else
        {
            DisplayTextBox.Text = DisplayTextBox.Text + ".";
        }
    }
}

5 个答案:

答案 0 :(得分:2)

向您的类添加一个布尔变量,该变量表示操作是否刚刚完成的标志,如下所示:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    Double FirstNumber;
    string mathOperator = "";
    bool operationJustCompleted = false;

最初,该值为false,因为首次创建计算器时没有发生任何事情。

现在,在等于按钮事件处理程序的末尾,将标志设置为true,如下所示:

private void EqualButton_Click(object sender, EventArgs e)
{
    // Logic for calculation

    operationJustCompleted = true;
}

最后,在数字按钮的事件处理程序中,检查operationJustCompleted标志是否为真,如果是,则清除文本并将刚刚完成的操作重置为false;像这样:

private void Button1_Click(object sender, EventArgs e)
{
    if(operationJustCompleted)
    {
        DisplayTextBox.Text = String.Empty;
        operationJustCompleted = false;
    }
    DisplayTextBox.Text = DisplayTextBox.Text + "1";
}

答案 1 :(得分:0)

向您的班级添加州变量。如果为true,并且选择了数字,请在写入数字之前清除显示并将其设置为false。

单击“=”时,将其设置为true。

答案 2 :(得分:0)

根据简要回顾,您似乎需要引入一个跟踪计算器状态的变量,然后当按下“相等”按钮时,将状态设置为计算值。然后,在数字按钮(0-9)中,您需要检查状态变量,并附加或替换显示文本。

答案 3 :(得分:0)

创建此全局变量...

private bool equationComplete = false;

将此添加到等于按钮的末尾...

equationComplete = true;

将其添加到每个数字按钮的开头,单击...

if (equationComplete) DisplayTextBox.Text = "";

将此添加到每个数字按钮单击事件处理程序的末尾...

equationComplete = false;

答案 4 :(得分:0)

就像David Arno所说的那样:

    private void EqualButton_Click(object sender, EventArgs e)
    {
        ClearDisplayBeforeNextTextEntry = true;
    }

    private void Button1_Click(object sender, EventArgs e)
    {
        // New code
        ClearText();
        // Old code
        DisplayTextBox.Text = DisplayTextBox.Text + "1";
    }

    // Same for all other number buttons as above

    private void ClearText()
    {
        if (ClearDisplayBeforeNextTextEntry)
        {
            DisplayTextBox.Text = "";
            ClearDisplayBeforeNextTextEntry = false;
        }
    }