简体   繁体   中英

C# Calculator, Equal button won't perform operation on second click

I'm building calculator (similar to win10 standard calculator), and problem is when you click equal button twice or more. I'm getting second argument from textBox and every time you click equal button it changes it's text value.

eg when you type 10 + 5 = 15 and then you press button again it shows 25 instead of 20 and so on.

    private void buttonEqual(object sender, EventArgs e)
    {

        secondArg = Convert.ToDouble(textBox1.Text);

        switch (oper) {

            case "+":
                    result = firstArg + Convert.ToDouble(label7.Text);
                    textBox1.Text = result.ToString();
                    label3.Text = firstArg.ToString();
                break;

            case "-":
                    result = firstArg - secondArg;
                    textBox1.Text = result.ToString();
                    label3.Text = result.ToString();
                break;

            case "*":
                    result = firstArg * secondArg;
                    textBox1.Text = result.ToString();
                    label3.Text = result.ToString();
                break;

            case "/":
                    result = firstArg / secondArg;
                    textBox1.Text = result.ToString();
                    label3.Text = result.ToString();
                break;

            case "^":
                result = Math.Pow(firstArg, secondArg);
                textBox1.Text = result.ToString();
                break;

In your + case, you set label3.Text = firstArg.ToString(); . In all the other cases shown you set it equal to result . Set label3.Text = result.ToString(); to fix.

You also don't use secondArg in the + case, this might be contributing to your issue as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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