How to create a Simple Calculator in C#?

Simple Calculator Program in C#

This program displays a calculator. It performs arithmetic operations like addition, subtraction, multiplication and division. The user interface is designed using Label and Button controls. The input and output are displayed in a Label. Buttons represent digits (0-9) and operators (+,-,* and /). To develop the program, do the following steps.
1. Start a new project in Visual Studio. (File Menu -> New -> Project…)
2. Click on “Visual C#” under “Installed Templates”.
3. Next, click on “Windows”
4. Next, click on “Windows Forms Application”
5. Type project name as “calc”
6. Click “OK” button.
7. Now, a blank Form will be displayed in the Screen.
8. In the right side of the Screen, the Properties Window is displayed.

9. Click on the “Text” property and Change it to “Simple Calculator”


10. Toolbox is displayed in the left side of the Form


11. Click on “Label” control in Toolbox.


12. Draw a rectangle on the Form. Now, a Label control is placed on the Form.
13. Now, the Form looks like below figure.

14. Select the Label control on the Form.
15. In properties Window, change the following properties for “Label” control
AutoSize = False
Font -> Font Size = 16
BackColor = any color as per your like.
TextAlign = MiddleRight

16. Click on “Button” control in the Toolbox.

17. Draw 16 buttons on the Form, as given in the below figure.
18. Select “Button1” control in the Form.
19. In Properties Window, Click on the “Text” property.
20. Change it to “1”.
21. Similarly, change the “Text” property for other 15 buttons, as given in the below figure.



Double Click on the Form to display Code Editor Window. Type the following program in Code Editor Window.

Program

namespace calc
{
    public partial class Form1 : Form
    {
        float a,b;
        int op;
        String s;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        a = 0;
        b = 0;
        op = 0;
        Label1.Text = "0";
        s = "";
        }

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

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

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

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

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

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

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

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

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

        private void Button10_Click(object sender, EventArgs e)
        {
            s = s + "0";
            Label1.Text = s;
        }

        private void Button11_Click(object sender, EventArgs e)
        {
        s = "";
        a = 0;
        b = 0;
        Label1.Text = "0";
        }

        private void Button12_Click(object sender, EventArgs e)
        {
        b = float.Parse(s);
        if (op == 1)  a = a + b;
        if (op == 2)  a = a - b;
        if (op == 3)  a = a * b;
        if (op == 4)  a = a / b;

        s = a.ToString();
        Label1.Text = s;
        }

        private void Button13_Click(object sender, EventArgs e)
        {
         op = 1;
         a = float.Parse(s);
         s = "";
        }

        private void Button14_Click(object sender, EventArgs e)
        {
            op = 2;
            a = float.Parse(s);
            s = "";
        }

        private void Button15_Click(object sender, EventArgs e)
        {
            op = 3;
            a = float.Parse(s);
            s = "";
        }

        private void Button16_Click(object sender, EventArgs e)
        {
            op = 4;
            a = float.Parse(s);
            s = "";
        }
    }
}

The above program is taken from the book "Learn C# Programming: A Practical Approach". This book is available in Amazon for Download.


To get the book from Amazon USA
https://www.amazon.com/dp/B083RT1MKJ

To get the book from Amazon India
https://www.amazon.in/dp/B083RT1MKJ

To buy Printed (paperpack) edition of the book
https://www.amazon.com/dp/B087L33CCM