How to create calculator in window application form c#

A simple calculator application in C# window form application. Here you will find easiest way to create a calculator in C# window form application.

Step 1.

Create a New window application form in Microsoft Visual Studio.

create new

Step 2.

Design your Window Application Form like below form.

calculator

Step 3.

Now write the below coding in your application.


    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 WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            string opr;
            double oparand1, oparand2, result;

            //button 1 coding
            private void button2_Click(object sender, EventArgs e)
            {
                textBox1.Text = textBox1.Text + 1;
            }

            //button 2 coding
            private void button3_Click(object sender, EventArgs e)
            {
                textBox1.Text = textBox1.Text + 2;
            }

            //button 3 coding
            private void button4_Click(object sender, EventArgs e)
            {
                textBox1.Text = textBox1.Text + 3;
            }

            //button 4 coding
            private void button5_Click(object sender, EventArgs e)
            {
                textBox1.Text = textBox1.Text + 4;
            }

            //button 5 coding
            private void button6_Click(object sender, EventArgs e)
            {
                textBox1.Text = textBox1.Text + 5;
            }

            //button 6 coding
            private void button7_Click(object sender, EventArgs e)
            {
                textBox1.Text = textBox1.Text + 6;
            }

            //button 7 coding
            private void button12_Click(object sender, EventArgs e)
            {
                textBox1.Text = textBox1.Text + 7;
            }

            //button 8 coding
            private void button11_Click(object sender, EventArgs e)
            {
                textBox1.Text = textBox1.Text + 8;
            }

            //button 9 coding
            private void button10_Click(object sender, EventArgs e)
            {
                textBox1.Text = textBox1.Text + 9;
            }

            //button 0 coding
            private void button9_Click(object sender, EventArgs e)
            {
                textBox1.Text = textBox1.Text + 0;
            }

            //dot button coding
            private void button18_Click(object sender, EventArgs e)
            {
                if (textBox1.Text.Contains("."))
                {
                    textBox1.Text = textBox1.Text;
                }
                else
                {
                    textBox1.Text = textBox1.Text + ".";
                }
            }

            private void button8_Click(object sender, EventArgs e)
            {
                textBox1.Text = textBox1.Text + 0+0;
            }

            //X button coding-clear code
            private void button1_Click(object sender, EventArgs e)
            {
                textBox1.Text = "";
            }

            //plus button coding
            private void button13_Click(object sender, EventArgs e)
            {
                oparand1 = Convert.ToDouble(textBox1.Text);
                opr = "+";
                textBox1.Clear();
            }

            //equal button coding
            private void button17_Click(object sender, EventArgs e)
            {
            oparand2 = Convert.ToDouble(textBox1.Text);
            switch (opr)
            {
            case "+": result = oparand1 + oparand2;
            textBox1.Text = Convert.ToString(result);
            break;

            case "-": 
            result = oparand1 - oparand2;
            textBox1.Text = Convert.ToString(result);
            break;
            
            case "*": 
            result = oparand1 * oparand2;
            textBox1.Text = Convert.ToString(result);
            break;
            
            case "/": 
            if (oparand2 == 0)
            {
            textBox1.Text = "0.0";
            break;
            }
            else
            {
            result = oparand1 / oparand2;
            textBox1.Text = Convert.ToString(result);
            break;
            }
            }
            }

            //multiply button coding
            private void button14_Click(object sender, EventArgs e)
            {
                oparand1 = Convert.ToDouble(textBox1.Text);
                opr = "*";
                textBox1.Clear();
            }

            //minus button coding
            private void button15_Click(object sender, EventArgs e)
            {
                oparand1 = Convert.ToDouble(textBox1.Text);
                opr = "-";
                textBox1.Clear();
            }

            //divide button coding
            private void button16_Click(object sender, EventArgs e)
            {
                oparand1 = Convert.ToDouble(textBox1.Text);
                opr = "/";
                textBox1.Clear();
            }        

        }
    }

Output:

window calculator

Watch Tutorial Video on YouTube

Watch and Subscribe Now

Post Your Comment