Create simple Calculator using windows form with c#

Posted on 26 May, 2019 by Administrator

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

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.

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.

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.

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

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

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.

We will repeat it with button 2 - 9 and +,-,*,/, = , ., C

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

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


Comments

New blog posts

String and number in python
String and number in python

8 June, 2019 by Administrator

1. String data type All programming...

Variables expressions and statements in python
Variables expressions and statements in python

2 June, 2019 by Administrator

1. Value and type Value is one of...

First python hello world
First python hello world

31 May, 2019 by Administrator

1. What is Python: Python is an Interpreter...

View all blog entries →

Give away $50 Digital Ocean