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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
double firstno = 0; // First number will initially zero
string sign = "+"; // Initial sign is "+" it may be "-","*","/"
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void BtnOne_Click(object sender, EventArgs e)
{
TxtCalc.Text += BtnOne.Text; // button 1 Text is "1"
}
private void BtnTwo_Click(object sender, EventArgs e)
{
TxtCalc.Text += BtnTwo.Text;
}
private void BtnThree_Click(object sender, EventArgs e)
{
TxtCalc.Text += BtnThree.Text;
}
private void BtnFour_Click(object sender, EventArgs e)
{
TxtCalc.Text += BtnFour.Text;
}
private void BtnFive_Click(object sender, EventArgs e)
{
TxtCalc.Text += BtnFive.Text;
}
private void BtnSix_Click(object sender, EventArgs e)
{
TxtCalc.Text += BtnSix.Text;
}
private void BtnSeven_Click(object sender, EventArgs e)
{
TxtCalc.Text += BtnSeven.Text;
}
private void BtnEight_Click(object sender, EventArgs e)
{
TxtCalc.Text += BtnEight.Text;
}
private void BtnNine_Click(object sender, EventArgs e)
{
TxtCalc.Text += BtnNine.Text;
}
private void BtnZero_Click(object sender, EventArgs e)
{
TxtCalc.Text += BtnZero.Text;
}
private void BtnDot_Click(object sender, EventArgs e)
{
TxtCalc.Text += BtnDot.Text;
}
private void BtnPlus_Click(object sender, EventArgs e) // Button Add Click Event
{
sign = "+"; // sign will be "+" for identification
firstno = Convert.ToDouble(TxtCalc.Text); //convert value in textbox to double and put it in firstno
TxtCalc.Text = "";
}
private void BtnSubstraction_Click(object sender, EventArgs e)// Button SUBTRACT Click Event
{
sign = "-";
firstno = Convert.ToDouble(TxtCalc.Text);
TxtCalc.Text = "";
}
private void BtnMultiplication_Click(object sender, EventArgs e)
{
sign = "*";
firstno = Convert.ToDouble(TxtCalc.Text);
TxtCalc.Text = "";
}
private void BtnDevided_Click(object sender, EventArgs e)
{
sign = "/";
firstno = Convert.ToDouble(TxtCalc.Text);
TxtCalc.Text = "";
}
private void BtnPercent_Click(object sender, EventArgs e)
{
sign = "%";
firstno = Convert.ToDouble(TxtCalc.Text);
TxtCalc.Text = "";
}
private void BtnEquals_Click(object sender, EventArgs e)
{
double secno;
double res = 0; //Initially result is zero(0)
secno = Convert.ToDouble(TxtCalc.Text); //convert value in textbox to double and put it in secno
if (sign == "+")
res = firstno + secno;
if (sign == "-")
res = firstno - secno;
if (sign == "*")
res = firstno * secno;
if (sign == "/")
res = firstno / secno;
if (sign == "*10")
res = Math.Pow(firstno, secno);
TxtCalc.Text = res.ToString();
}
private void BtnClear_Click(object sender, EventArgs e) //Reset button event
{
TxtCalc.Text = "";
}
private void button2_Click(object sender, EventArgs e) //Off click event
{
Application.Exit(); //used to exit application
}
}
}
OR
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 WindowsFormsApplication1
{
public partial class Form2 : Form
{
double opd1, opd2, res;
string op;
public Form2()
{
InitializeComponent();
}
private void Number_Click(object sender, EventArgs e)
{
System.Windows.Forms.Button Btn = sender as System.Windows.Forms.Button;
TxtCalc.Text += Btn.Text;
}
private void Operator_Click(object sender, EventArgs e)
{
opd1 =Convert.ToDouble(TxtCalc.Text);
System.Windows.Forms.Button Btn = sender as System.Windows.Forms.Button;
op= Btn.Text;
TxtCalc.Text = "";
}
private void BtnEquals_Click(object sender, EventArgs e)
{
opd2 = Convert.ToDouble(TxtCalc.Text);
switch (op)
{
case "+":
res = opd1 + opd2;
break;
case "-":
res = opd1 - opd2;
break;
case "*":
res = opd1 * opd2;
break;
case "/":
res = opd1 / opd2;
break;
}
TxtCalc.Text = res.ToString();
}
private void BtnClear_Click(object sender, EventArgs e)
{
TxtCalc.Text = "";
}
}
}
Note : You can add (Number_Click) in all Operator Click Event.
You can add (Operator_Click) in all Operator Click Event.
How to add this event in all button Events.
Just choose the button and press f4 after that just copy the event name and paste the click event.