VR Indian Wanderers Youtube RSS Feed
Learn with Vikas Suhag
Design a simple Calculator in C#
Nov 08, 2021   Vikas Suhag

Simple Calculator using C#

Simple Calculator Program using C#

Source Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
    public partial class Form1 : Form
    {
        int status = 0,st=0;
        double res, num1;
        String opstate;
        public Form1()
        {
            InitializeComponent();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (status == 0 && textBox1.Text.Length<12)
            {
                textBox1.Text = "7";

                status = 1;
            }
            else if (status == 1 && textBox1.Text.Length < 12)
            {
                textBox1.Text = textBox1.Text + "7";
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (status == 0 && textBox1.Text.Length < 12)
            {
                textBox1.Text = "3";
                status = 1;
            }
            else if (status == 1 && textBox1.Text.Length < 12)
            {
                textBox1.Text = textBox1.Text + "3";
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (status == 0 && textBox1.Text.Length < 12)
            {
                textBox1.Text = "9";
                status = 1;
            }
            else if (status == 1 && textBox1.Text.Length < 12)
            {
                textBox1.Text = textBox1.Text + "9";
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (status == 0 && textBox1.Text.Length < 12)
            {
                textBox1.Text = "8";
                status = 1;
            }
            else if (status == 1 && textBox1.Text.Length < 12)
            {
                textBox1.Text = textBox1.Text + "8";
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (status == 0 && textBox1.Text.Length < 12)
            {
                textBox1.Text = "6";
                status = 1;
            }
            else if (status == 1 && textBox1.Text.Length < 12)
            {
                textBox1.Text = textBox1.Text + "6";
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (status == 0 && textBox1.Text.Length < 12)
            {
                textBox1.Text = "5";
                status = 1;
            }
            else if (status == 1 && textBox1.Text.Length < 12)
            {
                textBox1.Text = textBox1.Text + "5";
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (status == 0 && textBox1.Text.Length < 12)
            {
                textBox1.Text = "4";
                status = 1;
            }
            else if (status == 1 && textBox1.Text.Length < 12)
            {
                textBox1.Text = textBox1.Text + "4";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (status == 0 && textBox1.Text.Length < 12)
            {
                textBox1.Text = "2";
                status = 1;
            }
            else if (status == 1 && textBox1.Text.Length < 12)
            {
                textBox1.Text = textBox1.Text + "2";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (status == 0 && textBox1.Text.Length < 12)
            {
                textBox1.Text = "1";
                status = 1;
            }
            else if (status == 1 && textBox1.Text.Length < 12)
            {
                textBox1.Text = textBox1.Text + "1";
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (status == 0 && textBox1.Text.Length < 12)
            {
                textBox1.Text = "0";
            }
            else if (status == 1 && textBox1.Text.Length < 12)
            {
                textBox1.Text = textBox1.Text + "0";
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            textBox1.Text = "0";
            st = status = 0;
            res = num1 = 0.0;
            opstate = "";
        }

        private void button16_Click(object sender, EventArgs e)
        {
            if (st == 0 && status == 1)
            {
                num1 = Int32.Parse(textBox1.Text);
                status = 0;
                st++;
            }
            else if (st == 1 && status == 1)
            {
                switch (opstate)
                {
                    case "plus":
                        {
                            res = num1 + Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "minus":
                        {
                            res = num1 - Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "mul":
                        {
                            res = num1 * Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "div":
                        {
                            res = num1 / Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
                status = 0;
                st++;
            }
            else if (st > 1 && status == 1)
            {
                switch (opstate)
                {
                    case "plus":
                        {
                            res = res + Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "minus":
                        {
                            res = res - Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "mul":
                        {
                            res = res * Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "div":
                        {
                            res = res / Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
                status = 0;
            }
            else
            {
                textBox1.Text = res.ToString();
            }
            opstate = "plus";
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (status == 1)
            {
                switch (opstate)
                {
                    case "plus":
                        {
                            if (st == 1)
                            {
                                res = num1 + Int32.Parse(textBox1.Text);
                                textBox1.Text = res.ToString();
                            }
                            else if (st > 1)
                            {
                                res = res + Int32.Parse(textBox1.Text);
                                textBox1.Text = res.ToString();
                            }
                            break;
                        }
                    case "minus":
                        {
                            if (st == 1)
                            {
                                res = num1 - Int32.Parse(textBox1.Text);
                                textBox1.Text = res.ToString();
                            }
                            else if (st > 1)
                            {
                                res = res - Int32.Parse(textBox1.Text);
                                textBox1.Text = res.ToString();
                            }
                            break;
                        }
                    case "mul":
                        {
                            if (st == 1)
                            {
                                res = num1 * Int32.Parse(textBox1.Text);
                                textBox1.Text = res.ToString();
                            }
                            else if (st > 1)
                            {
                                res = res * Int32.Parse(textBox1.Text);
                                textBox1.Text = res.ToString();
                            }
                            break;
                        }
                    case "div":
                        {
                            if (st == 1)
                            {
                                res = num1 / Int32.Parse(textBox1.Text);
                                textBox1.Text = res.ToString();
                            }
                            else if (st > 0)
                            {
                                res = res / Int32.Parse(textBox1.Text);
                                textBox1.Text = res.ToString();
                            }
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
            }
            opstate = "";
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (st == 0 && status == 1)
            {
                num1 = Int32.Parse(textBox1.Text);
                status = 0;
                st++;
            }
            else if (st == 1 && status == 1)
            {
                switch (opstate)
                {
                    case "plus":
                        {
                            res = num1 + Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "minus":
                        {
                            res = num1 - Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "mul":
                        {
                            res = num1 * Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "div":
                        {
                            res = num1 / Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
                status = 0;
                st++;
            }
            else if (st >= 1 && status == 1)
            {
                switch (opstate)
                {
                    case "plus":
                        {
                            res = res + Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "minus":
                        {
                            res = res - Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "mul":
                        {
                            res = res * Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "div":
                        {
                            res = res / Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
                status = 0;
            }
            else
            {
                textBox1.Text = res.ToString();
            }
            opstate="minus";        
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (st == 0 && status == 1)
            {
                num1 = Int32.Parse(textBox1.Text);
                status = 0;
                st++;
            }
            else if (st == 1 && status == 1)
            {
                switch (opstate)
                {
                    case "plus":
                        {
                            res = num1 + Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "minus":
                        {
                            res = num1 - Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "mul":
                        {
                            res = num1 * Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "div":
                        {
                            res = num1 / Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
                status = 0;
                st++;
            }
            else if (st > 1 && status == 1)
            {
                switch (opstate)
                {
                    case "plus":
                        {
                            res = res + Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "minus":
                        {
                            res = res - Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "mul":
                        {
                            res = res * Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "div":
                        {
                            res = res / Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
                status = 0;
            }
            else
            {
                textBox1.Text = res.ToString();
            }
            opstate = "mul";        
        }

        private void button14_Click(object sender, EventArgs e)
        {
            if (st == 0 && status == 1)
            {
                num1 = Int32.Parse(textBox1.Text);
                status = 0;
                st++;
            }
            else if (st == 1 && status == 1)
            {
                switch (opstate)
                {
                    case "plus":
                        {
                            res = num1 + Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "minus":
                        {
                            res = num1 - Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "mul":
                        {
                            res = num1 * Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "div":
                        {
                            res = num1 / Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
                status = 0;
                st++;
            }
            else if (st > 1 && status == 1)
            {
                switch (opstate)
                {
                    case "plus":
                        {
                            res = res + Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "minus":
                        {
                            res = res - Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "mul":
                        {
                            res = res * Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    case "div":
                        {
                            res = res / Int32.Parse(textBox1.Text);
                            textBox1.Text = res.ToString();
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
                status = 0;
            }
            else
            {
                textBox1.Text = res.ToString();
            }
            opstate = "div";        
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void button17_Click(object sender, EventArgs e)
        {
            int t = Int32.Parse(textBox1.Text);
            t = t / 10;
            textBox1.Text = t.ToString();
        }

        private void button18_Click(object sender, EventArgs e)
        {
            res = Math.Sqrt(Int32.Parse(textBox1.Text));
            textBox1.Text = res.ToString();
            status = 0;
            st = 1;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Focus();
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
           
        }
    }
}


Leave a Comment

* Email will not be published.
All comments are only visible after approval.
Most Viewed articles
Travel Blog - VR Indian Wanderers
VR Indian Wanderers
Govt. Jobs Portal
Govt. Jobs Portal
Free Chart Maker
Make free animated Charts from .CSV
Search
Youtube Channel
Podcast
Subscribe to Email Updates
Connect With Us
VR Indian Wanderers Youtube RSS Feed
© 2024 Learn with Vikas Suhag. All Rights Reserved.