Create simple Calculator using windows form with c#

Today, i will show you how to write a simple calculator using windows form by c#

Step 1: Open up your visual studio and create a new project whether you dont have visual studio, please download it Link: https://www.visualstudio.com/vs/visual-studio-express/

In this article, i use Visual Studio Ultimate 2013

Simple Calculator c#_1

Set its type to Windows Form Application Visual C# and set its project name whatever you want. (I named it Calculator_WF) Then press OK.

Simple Calculator c#_2

Step 2: Change the Text Property title or whatever you want (I named it Simple Calculator) so that the name of this form will change.

Simple Calculator c#_3

Step 3:Click from the ToolBox, choose a TextBox and drag it into the form, resize it. You can choose some backcolor and whatever font you want.

Simple Calculator c#_4

Step 4: Now we will display the default value “0” left of the text box. Right click in the text box and modify the Text Property and Text Align change to Right

Simple Calculator c#_5

Remember to change its name to txtDisplay or whatever you want so that we can add function for it easily

Simple Calculator c#_6

Step 5:From the ToolBox drag a button. Change its name to btn1, this will help us to identify which button was pressed. We also change its Text to “1”, its back color, font and resize it.

Simple Calculator c#_7

We will repeat it with button 2 – 9 and +,-,*,/, = , ., C
Simple Calculator c#_8

Step 6:Double Click on a button btn1 to go to its event. So we will write a function when everytime user click on its button, the txtDisplay will add value”1″ and display it.

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

We would repeat this step for button 2,3,4,5,6,7,8,9,0

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

        private void btn2_Click(object sender, EventArgs e)
        {
            if (txtdisplay.Text == "0" && txtdisplay.Text != null)
            {
                txtdisplay.Text = "2";
            }
            else
            {
                txtdisplay.Text = txtdisplay.Text + "2";
            }
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            if (txtdisplay.Text == "0" && txtdisplay.Text != null)
            {
                txtdisplay.Text = "3";
            }
            else
            {
                txtdisplay.Text = txtdisplay.Text + "3";
            }
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            if (txtdisplay.Text == "0" && txtdisplay.Text != null)
            {
                txtdisplay.Text = "4";
            }
            else
            {
                txtdisplay.Text = txtdisplay.Text + "4";
            }
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            if (txtdisplay.Text == "0" && txtdisplay.Text != null)
            {
                txtdisplay.Text = "5";
            }
            else
            {
                txtdisplay.Text = txtdisplay.Text + "5";
            }
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            if (txtdisplay.Text == "0" && txtdisplay.Text != null)
            {
                txtdisplay.Text = "6";
            }
            else
            {
                txtdisplay.Text = txtdisplay.Text + "6";
            }
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            if(txtdisplay.Text=="0" && txtdisplay.Text!=null)
            {
                txtdisplay.Text = "7";
            }
            else
            {
                txtdisplay.Text = txtdisplay.Text + "7";
            }
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            if (txtdisplay.Text == "0" && txtdisplay.Text != null)
            {
                txtdisplay.Text = "8";
            }
            else
            {
                txtdisplay.Text = txtdisplay.Text + "8";
            }
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            if (txtdisplay.Text == "0" && txtdisplay.Text != null)
            {
                txtdisplay.Text = "9";
            }
            else
            {
                txtdisplay.Text = txtdisplay.Text + "9";
            }
        }

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

Then run the application and click to test the button function
Simple Calculator c#_9

Step 7: We will handle the button C (Clear) by change the value txtDisplay to “0” when pressed it. And others button operator:

  • +: btnplus
  • -: btnminus
  • *: btnmutiply
  • /: btndivide
  • =: btnequal
  • .: btndot

Double click on each button operator and create its function

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_WF
{
    public partial class Form1 : Form
    {
        double FirstNumber;
        string Operation;
        public Form1()
        {
            InitializeComponent();
        }             

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

        private void btn2_Click(object sender, EventArgs e)
        {
            if (txtdisplay.Text == "0" && txtdisplay.Text != null)
            {
                txtdisplay.Text = "2";
            }
            else
            {
                txtdisplay.Text = txtdisplay.Text + "2";
            }
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            if (txtdisplay.Text == "0" && txtdisplay.Text != null)
            {
                txtdisplay.Text = "3";
            }
            else
            {
                txtdisplay.Text = txtdisplay.Text + "3";
            }
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            if (txtdisplay.Text == "0" && txtdisplay.Text != null)
            {
                txtdisplay.Text = "4";
            }
            else
            {
                txtdisplay.Text = txtdisplay.Text + "4";
            }
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            if (txtdisplay.Text == "0" && txtdisplay.Text != null)
            {
                txtdisplay.Text = "5";
            }
            else
            {
                txtdisplay.Text = txtdisplay.Text + "5";
            }
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            if (txtdisplay.Text == "0" && txtdisplay.Text != null)
            {
                txtdisplay.Text = "6";
            }
            else
            {
                txtdisplay.Text = txtdisplay.Text + "6";
            }
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            if(txtdisplay.Text=="0" && txtdisplay.Text!=null)
            {
                txtdisplay.Text = "7";
            }
            else
            {
                txtdisplay.Text = txtdisplay.Text + "7";
            }
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            if (txtdisplay.Text == "0" && txtdisplay.Text != null)
            {
                txtdisplay.Text = "8";
            }
            else
            {
                txtdisplay.Text = txtdisplay.Text + "8";
            }
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            if (txtdisplay.Text == "0" && txtdisplay.Text != null)
            {
                txtdisplay.Text = "9";
            }
            else
            {
                txtdisplay.Text = txtdisplay.Text + "9";
            }
        }

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

        private void btnC_Click(object sender, EventArgs e)
        {
            txtdisplay.Text = "0";
        }

        private void btndot_Click(object sender, EventArgs e)
        {
            txtdisplay.Text = txtdisplay.Text + ".";
        }

        private void btnplus_Click(object sender, EventArgs e)
        {
            FirstNumber = Convert.ToDouble(txtdisplay.Text);
            txtdisplay.Text = "0";
            Operation = "+";
        }

        private void btnminus_Click(object sender, EventArgs e)
        {
            FirstNumber = Convert.ToDouble(txtdisplay.Text);
            txtdisplay.Text = "0";
            Operation = "-";
        }

        private void btnmutiply_Click(object sender, EventArgs e)
        {
            FirstNumber = Convert.ToDouble(txtdisplay.Text);
            txtdisplay.Text = "0";
            Operation = "*";
        }

        private void btndivide_Click(object sender, EventArgs e)
        {
            FirstNumber = Convert.ToDouble(txtdisplay.Text);
            txtdisplay.Text = "0";
            Operation = "/";
        }

        private void btnequal_Click(object sender, EventArgs e)
        {
            double SecondNumber;
            double Result;
            SecondNumber = Convert.ToDouble(txtdisplay.Text);
            if (Operation == "+")
            {
                Result = (FirstNumber + SecondNumber);
                txtdisplay.Text = Convert.ToString(Result);
                FirstNumber = Result;
            }
            if (Operation == "-")
            {
                Result = (FirstNumber - SecondNumber);
                txtdisplay.Text = Convert.ToString(Result);
                FirstNumber = Result;
            }
            if (Operation == "*")
            {
                Result = (FirstNumber * SecondNumber);
                txtdisplay.Text = Convert.ToString(Result);
                FirstNumber = Result;
            }
            if (Operation == "/")
            {
                if(SecondNumber==0)
                {
                    txtdisplay.Text="Cannot divide zero";
                }
                else{
                    Result = (FirstNumber / SecondNumber);
                    txtdisplay.Text = Convert.ToString(Result);
                    FirstNumber = Result;
                }                
            }
        }

    }
}

Finaly we have a simple calculator, you can choose release it and run it in exe.
Download Project Via Github: https://github.com/anhnt4288/Simple-Calculator-Windows-Form

Add Comment