none
Simple calculator problem RRS feed

  • Question

  • I am trying to code a simple calculator for my college class. However every time i push a button nothing will pop up in the textbox. I am basing it off of a tutorial i found that coded it in 2010 c# and i am using 2017 c#, so there must be some sort of problem from that. Here is the code. 

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace Calculator
    {
        public partial class Form1 : Form
        {
            string input = string.Empty;
            string operand1 = string.Empty;
            string operand2 = string.Empty;
            char operation;
            double result = 0.0;

            public Form1()
            {
                InitializeComponent();
            }

            private void Zero_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += "0";
                this.textBox1.Text += input;
            }

            private void One_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += "1";
                this.textBox1.Text += input;
            }

            private void Two_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += "2";
                this.textBox1.Text += input;
            }

            private void Three_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += "3";
                this.textBox1.Text += input;
            }

            private void Four_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += "4";
                this.textBox1.Text += input;
            }

            private void Five_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += "5";
                this.textBox1.Text += input;
            }

            private void Six_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += "6";
                this.textBox1.Text += input;
            }

            private void Seven_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += "7";
                this.textBox1.Text += input;
            }

            private void Eight_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += "8";
                this.textBox1.Text += input;
            }

            private void Nine_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += "9";
                this.textBox1.Text += input;
            }

            private void Decimal_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += ".";
                this.textBox1.Text += input;
            }

            private void Divide_Click(object sender, EventArgs e)
            {
                operand1 = input;
                operation = '/';
                input = string.Empty;
            }

            private void Multiply_Click(object sender, EventArgs e)
            {
                operand1 = input;
                operation = '*';
                input = string.Empty;
            }

            private void Plus_Click(object sender, EventArgs e)
            {
                operand1 = input;
                operation = '+';
                input = string.Empty;
            }

            private void Minus_Click(object sender, EventArgs e)
            {
                operand1 = input;
                operation = '-';
                input = string.Empty;
            }

            private void Clear_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                this.input = string.Empty;
                this.operand1 = string.Empty;
                this.operand2 = string.Empty;
            }

            private void Equal_Click(object sender, EventArgs e)
            {
                operand2 = input;
                double.TryParse(operand1, out double num1);
                double.TryParse(operand2, out double num2);
                
                if (operation == '/')
                {
                    result = num1 / num2;
                    textBox1.Text = result.ToString();
                }
                else if (operation == '*')
                {
                    result = num1 * num2;
                    textBox1.Text = result.ToString();
                }
                else if (operation == '+')
                {
                    result = num1 + num2;
                    textBox1.Text = result.ToString();
                }
                else if (operation == '-')
                {
                    if (num2 != 0)
                    {
                        result = num1 / num2;
                        textBox1.Text = result.ToString();
                    }
                    else
                    {
                        textBox1.Text = "ERROR: cannot divide by 0";
                    }
                }
            }
        }
    }

    Wednesday, January 17, 2018 11:04 PM

All replies

  • Have you subscribed each method to the appropriate click event? That is, in the Designer.cs do you have a line like

    this.Zer0.Click += new System.EventHandler(this.Zero_Click);  // This is for the Zero button. There should be something similar for the other buttons.

    Wednesday, January 17, 2018 11:20 PM
  • Hello cpdf98,

    You should bind method to each button click event. Because there are some methods and the button doesn't know which method should be executed. Try to set as below. The way equals Ante's approach.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 18, 2018 1:50 AM
    Moderator
  • Hello cpdf98,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Thank you for your understanding and cooperation.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, January 28, 2018 1:18 PM
    Moderator