C# Windows Form App Calculator

In this tutorial I will show you how to create a simple calculator app using C#. You will also learn how to use enum to enumerate a possible values.

Advertisements

 

 

First Create a new application and design your form to look like this.

ui design'

I named my textbox as txtDisplay.

Then my button one to nine was named button1, button2, button3, button4, button5, button6, button7, button8, button9.

Then button for zero was named button10. For the point it was named button11. Then the clear button was named button12.

The addition, subtraction, multiplication, division and equals. It was named btnAddbtnSubtractbtnMultiplybtnDivide, and btnEqual.

here is the code for the buttons 0 to 9 and the point.

private void Button1_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += button1.Text;
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += button2.Text;
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += button3.Text;
        }

        private void Button4_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += button4.Text;
        }

        private void Button5_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += button5.Text;
        }

        private void Button6_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += button6.Text;
        }

        private void Button7_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += button7.Text;
        }

        private void Button8_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += button8.Text;
        }

        private void Button9_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += button9.Text;
        }

        private void Button10_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += button10.Text;
        }

        private void Button11_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += button11.Text;
        }

Now add a new class and name it Operation.cs. Then change the code to this.

public enum Operation
    {
        Addition,
        Subtraction,
        Multiplication,
        Division,
        None
    }

We used enum to enumerate possible operations that can be executed in out application.

Now back to our Form1 code. Declare a class variable named total which is a type double and operation which is type of enum that we created earlier.

        double total = 0;
        Operation operation = Operation.None;

Now let’s create a method called Compute.

private double Compute()
        {
            double result = double.Parse(txtDisplay.Text);
            if (operation == Operation.Addition)
            {
                result = total + double.Parse(txtDisplay.Text);
            }
            else if (operation == Operation.Subtraction)
            {
                result = total - double.Parse(txtDisplay.Text);
            }
            else if (operation == Operation.Multiplication)
            {
                result = total * double.Parse(txtDisplay.Text);
            }
            else if (operation == Operation.Division)
            {
                result = total / double.Parse(txtDisplay.Text);
            }
            txtDisplay.Clear();
            return result;
        }

The compute method is responsible of executing the selected operation.
Then here is code for our operation buttons.

private void BtnAdd_Click(object sender, EventArgs e)
        {
            total = Compute();
            operation = Operation.Addition;
        }

        private void BtnSubtract_Click(object sender, EventArgs e)
        {
            total = Compute();
            operation = Operation.Subtraction;
        }

        private void BtnMultiply_Click(object sender, EventArgs e)
        {
            total = Compute();
            operation = Operation.Multiplication;
        }

        private void BtnDivide_Click(object sender, EventArgs e)
        {
            total = Compute();
            operation = Operation.Division;
        }

Then for equal button. We will check if there is an operation. If there is, then we will execute the opetation by calling the Compute method then display the value to our txtDispaly.

private void BtnEqual_Click(object sender, EventArgs e)
        {
            if(operation != Operation.None)
            {
                txtDisplay.Text = Compute().ToString();
                operation = Operation.None;
                total = 0;
            }
        }

Then in our clear button. We will clear the value of txtDispaly and set total to 0 and operation to operation.None.

private void Button12_Click(object sender, EventArgs e)
        {
            txtDisplay.Clear();
            total = 0;
            operation = Operation.None;
        }

Then run your application and you should now have a working calculator.output

Source code: https://github.com/alexcalingasan/SimpleCalculator

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s