How to Build/Make a Calculator in C# using window form ?

How to build/make a Calculator in C# ?

Here is the screenshot of our output at the end of this discussion that is about that how to build a calculator in C# using window form:


So this is our output and I will design my window form like this and here are the tools used from the toolbox given for window form:

                                         Interface Design For Our Calculator
So first design your interface like this using Label, TextBox Buttons with same "name" because I will use Button class to create same object  and same event handler for all buttons. So here is the back end code for this calculator:
⏩  
      
using System;
using System.Windows.Forms;

namespace TR_Calculator
{
    public partial class Form1 : Form
    { 

      //initialize the variables
        Double resultValue = 0;
        String operationPerformed = "";
        bool isOperationPrformed = false;

        public Form1()
        {
            InitializeComponent();
        }

       //event that occur when any digit button is clicked
        private void btn1_Click(object sender, EventArgs e)
        {
            try{
            if ((txtResult.Text == "0")|| isOperationPrformed)
                txtResult.Clear();
            isOperationPrformed = false;
            Button button = (Button)sender;
            if(button.Text==".")
            {
                if(!txtResult.Text.Contains("."))
                    txtResult.Text = txtResult.Text + button.Text;
            }
            else
            txtResult.Text = txtResult.Text + button.Text;

             
           // sound a beep
            Console.Beep((int)523.2, 100);
        }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

       //event that occur when an equal button is clicked
        private void opr_Click(object sender, EventArgs e)
        {
            try
            {
                Button button = (Button)sender;
                if (resultValue != 0)
                {
                    btnequal.PerformClick();
                    operationPerformed = button.Text;
                    labelCurrentOperation.Text = resultValue + " " + operationPerformed;
                    isOperationPrformed = true;
                }
                else
                {
                    operationPerformed = button.Text;
                    resultValue = Double.Parse(txtResult.Text);
                    labelCurrentOperation.Text = resultValue + " " + operationPerformed;
                    isOperationPrformed = true;
                }
               
                Console.Beep((int)523.2, 100);

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

       // event for C button
        private void btnc_Click(object sender, EventArgs e)
        {
            try
            {
                txtResult.Text = "0";
                Console.Beep((int)523.2, 100);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

// Event for CE Button
        private void btnce_Click(object sender, EventArgs e)
        {
            try
            {
                txtResult.Text = "0";
                resultValue = 0;
                Console.Beep((int)523.2, 100);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        // event for operators plus, minus etc.
        private void btnequal_Click(object sender, EventArgs e)
        {
            try
            {
                switch (operationPerformed)
                {
                    case "+":
                        txtResult.Text = (resultValue + Double.Parse(txtResult.Text)).ToString();
                        break;
                    case "-":
                        txtResult.Text = (resultValue - Double.Parse(txtResult.Text)).ToString();
                        break;
                    case "*":
                        txtResult.Text = (resultValue * Double.Parse(txtResult.Text)).ToString();
                        break;
                    case "/":
                        txtResult.Text = (resultValue / Double.Parse(txtResult.Text)).ToString();
                        break;
                    default:
                        break;
                }
                resultValue = Double.Parse(txtResult.Text);
                labelCurrentOperation.Text = "";   
                Console.Beep((int)523.2, 100);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}


So above is the code for calculator with understandable comments so run this code and enjoy your application. So for any help and suggestions please comment below follow my blog for interesting application in C# that will surely motivate your learning.

Here is the video description of this calculator:

Video Tutorial



Comments

Popular posts from this blog

How to build a CGPA Calculator in C# using Window Forms ?

How to change back color of window form using Track Bars in C#?