Calculator in Winform DotNet using C Sharp

Category > WINFORMS || Published on : Sunday, August 14, 2016 || Views: 10423 || Calculator Winform DotNet using C Sharp


Introduction

Here Pawan Kumar will explain how to create a Calculator in Winform DotNet using C Sharp

Description

In previous post I have explained Top Popular Open Source .Net Projects - Websites/Portals : Portal/CMS, Top Popular Open Source Forum ASP.Net Projects, Show Confirm Navigation using onbeforeunload event in javascript, MySQL Tutorial, Mvc 4 Razor CRUD Operation using C# || ASP.NET MVC 4 application to Create/Insert,Read,Update,Delete and Search functionality using Razor view engine and Entity Framework, Top Popular Open Source E-Commerce ASP.Net Projects, and many more articles.

Now I will explain How to Calculator in Winform DotNet using C Sharp

So follow the steps to learn Calculator in Winform DotNet using C Sharp

Below is the winform source codes

using System;

using System.Windows.Forms;

namespace calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        double val1, val2, result;
        int op, x,z,eq; //x is used for checking whether operator is clicked or not. // z is used for checking of multiple clicks of operator buttons.

        private void btn1_Click(object sender, EventArgs e)
        {
            if (x > 0)
            {
                textBox1.Text = btn1.Text;
                x = 0;
            }
            else
            {
                textBox1.Text = textBox1.Text + btn1.Text;
            }
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            if (x > 0)
            {
                textBox1.Text = btn2.Text;
                x = 0;
            }
            else
            {
                textBox1.Text = textBox1.Text + btn2.Text;
            }
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            if (x > 0)
            {
                textBox1.Text = btn3.Text;
                x = 0;
            }
            else
            {
                textBox1.Text = textBox1.Text + btn3.Text;
            }
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            if (x > 0)
            {
                textBox1.Text = btn4.Text;
                x = 0;
            }
            else
            {
                textBox1.Text = textBox1.Text + btn4.Text;
            }
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            if (x > 0)
            {
                textBox1.Text = btn5.Text;
                x = 0;
            }
            else
            {
                textBox1.Text = textBox1.Text + btn5.Text;
            }
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            if (x > 0)
            {
                textBox1.Text = btn6.Text;
                x = 0;
            }
            else
            {
                textBox1.Text = textBox1.Text + btn6.Text;
            }
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            if (x > 0)
            {
                textBox1.Text = btn7.Text;
                x = 0;
            }
            else
            {
                textBox1.Text = textBox1.Text + btn7.Text;
            }
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            if (x > 0)
            {
                textBox1.Text = btn8.Text;
                x = 0;
            }
            else
            {
                textBox1.Text = textBox1.Text + btn8.Text;
            }
        }
        private void btn9_Click(object sender, EventArgs e)
        {
            if (x > 0)
            {
                textBox1.Text = btn9.Text;
                x = 0;
            }
            else
            {
                textBox1.Text = textBox1.Text + btn9.Text;
            }
         
        }

        private void btn0_Click(object sender, EventArgs e)
        {
            if (x > 0)
            {
                textBox1.Text = btn0.Text;
                x = 0;
            }
            else
            {
                textBox1.Text = textBox1.Text + btn0.Text;
            }
        }

        private void btnPlus_Click(object sender, EventArgs e)
        {
            if (z > 0)
            {
                val2 = double.Parse(textBox1.Text);
                if (op == 1)
                { result = val1 + val2; }
                else if (op == 2)
                { result = val1 - val2; }
                else if (op == 3)
                { result = val1 * val2; }
                else if (op == 4)
                { result = val1 / val2; }
                textBox1.Text = result.ToString();
           
            }
           
            val1 = double.Parse(textBox1.Text);
            op = 1;
            x = 1;
            z = 1;
            eq = 0;
        }

        private void btnMinus_Click(object sender, EventArgs e)
        {

            if (z > 0)
            {
                val2 = double.Parse(textBox1.Text);
                if (op == 1)
                { result = val1 + val2; }
                else if (op == 2)
                { result = val1 - val2; }
                else if (op == 3)
                { result = val1 * val2; }
                else if (op == 4)
                { result = val1 / val2; }
                textBox1.Text = result.ToString();
         
            }
                val1 = double.Parse(textBox1.Text);
                op = 2;
                x = 1;
                z = 1;
                eq = 0;
        }

        private void btnMultiply_Click(object sender, EventArgs e)
        {
            if (z > 0)
            {
                val2 = double.Parse(textBox1.Text);
                if (op == 1)
                { result = val1 + val2; }
                else if (op == 2)
                { result = val1 - val2; }
                else if (op == 3)
                { result = val1 * val2; }
                else if (op == 4)
                { result = val1 / val2; }
                textBox1.Text = result.ToString();
         
            }
                val1 = double.Parse(textBox1.Text);
                op = 3;
                x = 1;
                z = 1;
                eq = 0;
        }

        private void btnDivision_Click(object sender, EventArgs e)
        {
            if (z > 0)
            {
                val2 = double.Parse(textBox1.Text);
                if (op == 1)
                { result = val1 + val2; }
                else if (op == 2)
                { result = val1 - val2; }
                else if (op == 3)
                { result = val1 * val2; }
                else if (op == 4)
                { result = val1 / val2; }
                textBox1.Text = result.ToString();
         
          }
         
                val1 = double.Parse(textBox1.Text);
                op = 4;
                x = 1;
                z = 1;
                eq = 0;
        }

        private void btnEqual_Click(object sender, EventArgs e)
        {
            if (eq > 0)
            {
                val1 = double.Parse(textBox1.Text);
            }
            else
            {
                eq = 1;
                val2 = double.Parse(textBox1.Text);
            }
                if (op == 1)
                { result = val1 + val2; }
                else if (op == 2)
                { result = val1 - val2; }
                else if (op == 3)
                { result = val1 * val2; }
                else if (op == 4)
                { result = val1 / val2; }
                textBox1.Text = result.ToString();
                x = 1;
                z = 0;
        }

        private void btnBackspace_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 0)
            {
                textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
                if (textBox1.Text.Length > 0)
                {
                    char[] a = textBox1.Text.ToCharArray();
                    if(a[a.Length-1]=='.')
                    {
                        textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
                    }
                }
            }
        }

        private void btnDot_Click(object sender, EventArgs e)
        {
            if (!textBox1.Text.Contains("."))
            {
                textBox1.Text = textBox1.Text + btnDot.Text;
            }
        }
    }
}

 

Conclusion:

So, In this tutorial we have learned, Calculator in Winform DotNet using C Sharp