Create Basic Calculator Using Windows Forms And C#

This app is capable of performing addition, subtraction, multiplication as well as division. So let’s get started.

Step 1

Open your Visual Studio or Visual C# Express Edition and create a new project.

Set its type to Windows Forms Application and set its name to CalculatorApp. Press OK.

You should see the main form on the workspace as follows:

Step 2

Change the form text property to Calculator, because we don’t want our application to have the title Form1 when it starts.

Step 3

From the ToolBox, drop a TextBox control onto the form, and resize it. Perform some changes in properties as shown in figure.

Step 4

Now, we start working with the display, usually when the calculator starts it should display the number 0. In our case it does not. So we modify the Text Property and write 0 in that. Make sure you don’t add any spaces before or after the 0.

The second thing to note is that the number is aligned to the left, while calculators align the number to the right. Search for the TextAlign property and change it to Right.

This is what the window will look like.

Step 5

From the tool box window, drag and drop a Button onto the form. Change its Name property to n1. This will help us identify which number was pressed. Change the backcolor property for the button. After the color changes, we will modify the text that the button is displaying, so change the text property into 1. Go to the font property, and set its font to courier new, and size to 16 for example.

Now we can repeat the same operation to all the other nine buttons, or we can just copy this button and get the same result quickly. Just hold the ctrl key and drag the control.

Change the names of the buttons (or you can leave them as they are and skip this part). The names will continue to be n2, n3, n4, n5, n6, n7, n8, n9, and n0.The window should look like this.

Step 6

Double click the button n1 to go to its event. The n1_Click is the name of this procedure. It tells you that it get executed when the button whose name n1 is clicked by the user. Write down the code to have it like this:

private void n1_Click(object sender, EventArgs e)  
{  
    if (textBox1.Text == "0" && textBox1.Text != null)  
    {  
        textBox1.Text = "1";  
    }  
    else  
    {  
        textBox1.Text = textBox1.Text + "1";  
    }  
}

You should repeat the code for buttons n2,n3,n4,n5,n6,n7,n8,n9, and n0 to add the numbers 2,3,4,5,6,7,8,9,0 respectively. So the code should look like this:  

private void n1_Click(object sender, EventArgs e)  
{  
    if (textBox1.Text == "0" && textBox1.Text != null)  
    {  
        textBox1.Text = "1";  
    }  
    else  
    {  
        textBox1.Text = textBox1.Text + "1";  
    }  
}  
private void n2_Click(object sender, EventArgs e)  
{  
    if (textBox1.Text == "0" && textBox1.Text != null)  
    {  
        textBox1.Text = "2";  
    }  
    else  
    {  
        textBox1.Text = textBox1.Text + "2";  
    }  
}  
  
private void n3_Click(object sender, EventArgs e)  
{  
    if (textBox1.Text == "0" && textBox1.Text != null)  
    {  
        textBox1.Text = "3";  
    }  
    else  
    {  
        textBox1.Text = textBox1.Text + "3";  
    }  
}  
  
private void n4_Click(object sender, EventArgs e)  
{  
    if (textBox1.Text == "0" && textBox1.Text != null)  
    {  
        textBox1.Text = "4";  
    }  
    else  
    {  
        textBox1.Text = textBox1.Text + "4";  
    }  
}  
  
private void n5_Click(object sender, EventArgs e)  
{  
    if (textBox1.Text == "0" && textBox1.Text != null)  
    {  
        textBox1.Text = "5";  
    }  
    else  
    {  
        textBox1.Text = textBox1.Text + "5";  
    }  
}  
  
private void n6_Click(object sender, EventArgs e)  
{  
    if (textBox1.Text == "0" && textBox1.Text != null)  
    {  
        textBox1.Text = "6";  
    }  
    else  
    {  
        textBox1.Text = textBox1.Text + "6";  
    }  
}  
  
private void n7_Click(object sender, EventArgs e)  
{  
    if (textBox1.Text == "0" && textBox1.Text != null)  
    {  
        textBox1.Text = "7";  
    }  
    else  
    {  
        textBox1.Text = textBox1.Text + "7";  
    }  
}  
private void n8_Click(object sender, EventArgs e)  
{  
    if (textBox1.Text == "0" && textBox1.Text != null)  
    {  
        textBox1.Text = "8";  
    }  
    else  
    {  
        textBox1.Text = textBox1.Text + "8";  
    }  
}  
private void n9_Click(object sender, EventArgs e)  
{  
    if (textBox1.Text == "" && textBox1.Text != null)  
    {  
        textBox1.Text = "9";  
    }  
    else  
    {  
        textBox1.Text = textBox1.Text + "9";  
    }  
}  
  
private void n0_Click(object sender, EventArgs e)  
{  
    textBox1.Text = textBox1.Text + "0";  
}

Now run the application and click few buttons:

Step 7

Next we create the clear button, and place the copy next to the 0. Change its name to bc, and text property to C. Similarly create add, sub, mul and div button as shown below:

Change the text property.name them as follows. 

  • + name is bad
  • - name is bsub
  • * name is bmult
  • / name is bdiv
  • = name is bequal

Step 8

Double click on each of the event handler and the code. So our final code should look like this":

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Text;  
using System.Windows.Forms;  
  
namespace CalculatorApp  
{  
    public partial class Form1 : Form  
    {  
        double FirstNumber;  
        string Operation;  
        public Form1()  
        {  
            InitializeComponent();  
        }  
  
         
        private void n1_Click(object sender, EventArgs e)  
        {  
            if (textBox1.Text == "0" && textBox1.Text != null)  
            {  
                textBox1.Text = "1";  
            }  
            else  
            {  
                textBox1.Text = textBox1.Text + "1";  
            }  
        }  
  
        private void n2_Click(object sender, EventArgs e)  
        {  
            if (textBox1.Text == "0" && textBox1.Text != null)  
            {  
                textBox1.Text = "2";  
            }  
            else  
            {  
                textBox1.Text = textBox1.Text + "2";  
            }  
        }  
  
        private void n3_Click(object sender, EventArgs e)  
        {  
            if (textBox1.Text == "0" && textBox1.Text != null)  
            {  
                textBox1.Text = "3";  
            }  
            else  
            {  
                textBox1.Text = textBox1.Text + "3";  
            }  
        }  
  
        private void n4_Click(object sender, EventArgs e)  
        {  
            if (textBox1.Text == "0" && textBox1.Text != null)  
            {  
                textBox1.Text = "4";  
            }  
            else  
            {  
                textBox1.Text = textBox1.Text + "4";  
            }  
        }  
  
        private void n5_Click(object sender, EventArgs e)  
        {  
            if (textBox1.Text == "0" && textBox1.Text != null)  
            {  
                textBox1.Text = "5";  
            }  
            else  
            {  
                textBox1.Text = textBox1.Text + "5";  
            }  
        }  
  
        private void n6_Click(object sender, EventArgs e)  
        {  
            if (textBox1.Text == "0" && textBox1.Text != null)  
            {  
                textBox1.Text = "6";  
            }  
            else  
            {  
                textBox1.Text = textBox1.Text + "6";  
            }  
        }  
  
        private void n7_Click(object sender, EventArgs e)  
        {  
            if (textBox1.Text == "0" && textBox1.Text != null)  
            {  
                textBox1.Text = "7";  
            }  
            else  
            {  
                textBox1.Text = textBox1.Text + "7";  
            }  
        }  
  
        private void n8_Click(object sender, EventArgs e)  
        {  
            if (textBox1.Text == "0" && textBox1.Text != null)  
            {  
                textBox1.Text = "8";  
            }  
            else  
            {  
                textBox1.Text = textBox1.Text + "8";  
            }  
        }  
  
        private void n9_Click(object sender, EventArgs e)  
        {  
            if (textBox1.Text == "" && textBox1.Text != null)  
            {  
                textBox1.Text = "9";  
            }  
            else  
            {  
                textBox1.Text = textBox1.Text + "9";  
            }  
        }  
  
        private void n0_Click(object sender, EventArgs e)  
        {  
            textBox1.Text = textBox1.Text + "0";  
        }  
  
        private void bad_Click(object sender, EventArgs e)  
        {  
            FirstNumber = Convert.ToDouble(textBox1.Text);  
            textBox1.Text = "0";  
            Operation = "+";  
        }  
  
        private void bsub_Click(object sender, EventArgs e)  
        {  
            FirstNumber = Convert.ToDouble(textBox1.Text);  
            textBox1.Text = "0";  
            Operation = "-";  
  
        }  
  
        private void bmul_Click(object sender, EventArgs e)  
        {  
            FirstNumber = Convert.ToDouble(textBox1.Text);  
            textBox1.Text = "0";  
            Operation = "*";  
        }  
  
        private void bdiv_Click(object sender, EventArgs e)  
        {  
            FirstNumber = Convert.ToDouble(textBox1.Text);  
            textBox1.Text = "0";  
            Operation = "/";  
        }  
  
        private void bc_Click(object sender, EventArgs e)  
        {  
            textBox1.Text = "0";  
        }  
  
        private void ndot_Click(object sender, EventArgs e)  
        {  
            textBox1.Text = textBox1.Text + ".";  
        }  
  
        private void nequal_Click(object sender, EventArgs e)  
        {  
            double SecondNumber;  
            double Result;  
  
            SecondNumber = Convert.ToDouble(textBox1.Text);  
  
            if (Operation == "+")  
            {  
                Result = (FirstNumber + SecondNumber);  
                textBox1.Text = Convert.ToString(Result);  
                FirstNumber = Result;  
            }  
            if (Operation == "-")  
            {  
                Result = (FirstNumber - SecondNumber);  
                textBox1.Text = Convert.ToString(Result);  
                FirstNumber = Result;  
            }  
            if (Operation == "*")  
            {  
                Result = (FirstNumber * SecondNumber);  
                textBox1.Text = Convert.ToString(Result);  
                FirstNumber = Result;  
            }  
            if (Operation == "/")  
            {  
                if (SecondNumber == 0)  
                {  
                    textBox1.Text = "Cannot divide by zero";  
  
                }  
                else  
                {  
                    Result = (FirstNumber / SecondNumber);  
                    textBox1.Text = Convert.ToString(Result);  
                    FirstNumber = Result;  
                }  
            }  
        }
    }  
}

Step 9

Finally we will modify some form behavior so that it does not change size.

  1. Change the FormBorderStyle property from Sizable to FixedSingle, this will prevent your calculator from being resized.
  2. Finally change the MaximizeBox for your form to be False. This prevents the form from being maximized.
  3. Change Backcolor property as you like.
  4. Add a status strip label by dragging and dropping it from the toolbox and change it’s text property as you like.

So this is how our calculator should look.