How to make simple Calculator in c#

Introduction

In this blog we will learn that how to make a basic calculator check the source code.

Basically in normal calculators u can use only buttons to execute the calculator in .net application but here is a liitle change in the scenario i added a hotkeys in this calculator (Like '+' to add ,'/' to divide) after press the numlock on button on calculator u can use numpad keys to execute this calculator so use it and enjoy the difference.

Example

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication33
{
    public partial class Form1 : Form
    {
        private string fun, no;

        public Form1()
        {
            InitializeComponent();
        }

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

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

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == string.Empty)
            {
                errorProvider1.SetError(textBox1, "Must enter the value to remove");
            }
            else
            {
                errorProvider1.Clear();
                no = textBox1.Text;
                int no1 = no.Length;
                textBox1.Text = (no.Substring(0, no1 - 1));
            }
        }

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

        // ... (similar formatting for the other button click event handlers)

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            if (textBox1.Text == "+" || textBox1.Text == "-" || textBox1.Text == "*" || textBox1.Text == "/")
            {
                textBox1.Text = "";
            }
        }

        // ... (similar formatting for the other methods)

        private void button21_Click(object sender, EventArgs e)
        {
            textBox1.Text = Convert.ToString(1 / Convert.ToDecimal(textBox1.Text));
        }

        private void button22_Click(object sender, EventArgs e)
        {
            // Handle button22 click
        }
    }
}
Next Recommended Reading Simple Calculator Console App Using C#