using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Globalization;
namespace Calculator
{
/// <summary>
/// Summary description for frmCalculator.
/// </summary>
public class frmCalculator : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnEight;
private System.Windows.Forms.Button btnNine;
private System.Windows.Forms.Button btnDivide;
private System.Windows.Forms.Button btnSeven;
private System.Windows.Forms.Button btnFive;
private System.Windows.Forms.Button btnSix;
private System.Windows.Forms.Button btnMultiply;
private System.Windows.Forms.Button btnFour;
private System.Windows.Forms.Button btnDot;
private System.Windows.Forms.Button btnPlusMinus;
private System.Windows.Forms.Button btnPlus;
private System.Windows.Forms.Button btnZero;
private System.Windows.Forms.Button btnTwo;
private System.Windows.Forms.Button btnThree;
private System.Windows.Forms.Button btnMinus;
private System.Windows.Forms.Button btnOne;
private System.Windows.Forms.Button btnBack;
private System.Windows.Forms.Button btnEqual;
private System.Windows.Forms.Button btnClear;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public delegate void ValueChangedEvent(object sender, double value);
public event ValueChangedEvent ValueChanged=null;
public event KeyPressEventHandler NumberKeyPressed=null;
public delegate void ErrorEvent(object sender, string Message);
public event ErrorEvent ErrorOccured=null;
private string clickedOperator="";
private double firstValue;
private double previousValue;
public frmCalculator()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
this.btnDot.Text=CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnEight = new System.Windows.Forms.Button();
this.btnNine = new System.Windows.Forms.Button();
this.btnDivide = new System.Windows.Forms.Button();
this.btnSeven = new System.Windows.Forms.Button();
this.btnFive = new System.Windows.Forms.Button();
this.btnSix = new System.Windows.Forms.Button();
this.btnMultiply = new System.Windows.Forms.Button();
this.btnFour = new System.Windows.Forms.Button();
this.btnDot = new System.Windows.Forms.Button();
this.btnPlusMinus = new System.Windows.Forms.Button();
this.btnPlus = new System.Windows.Forms.Button();
this.btnZero = new System.Windows.Forms.Button();
this.btnTwo = new System.Windows.Forms.Button();
this.btnThree = new System.Windows.Forms.Button();
this.btnMinus = new System.Windows.Forms.Button();
this.btnOne = new System.Windows.Forms.Button();
this.btnBack = new System.Windows.Forms.Button();
this.btnEqual = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnEight
//
this.btnEight.BackColor = System.Drawing.Color.WhiteSmoke;
this.btnEight.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnEight.Location = new System.Drawing.Point(32, 19);
this.btnEight.Name = "btnEight";
this.btnEight.Size = new System.Drawing.Size(20, 20);
this.btnEight.TabIndex = 7;
this.btnEight.TabStop = false;
this.btnEight.Text = "8";
this.btnEight.Click += new System.EventHandler(this.btnEight_Click);
this.btnEight.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
//
// btnNine
//
this.btnNine.BackColor = System.Drawing.Color.WhiteSmoke;
this.btnNine.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnNine.Location = new System.Drawing.Point(54, 19);
this.btnNine.Name = "btnNine";
this.btnNine.Size = new System.Drawing.Size(20, 20);
this.btnNine.TabIndex = 6;
this.btnNine.TabStop = false;
this.btnNine.Text = "9";
this.btnNine.Click += new System.EventHandler(this.btnNine_Click);
this.btnNine.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
//
// btnDivide
//
this.btnDivide.BackColor = System.Drawing.Color.DarkGray;
this.btnDivide.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnDivide.Location = new System.Drawing.Point(76, 19);
this.btnDivide.Name = "btnDivide";
this.btnDivide.Size = new System.Drawing.Size(20, 20);
this.btnDivide.TabIndex = 5;
this.btnDivide.TabStop = false;
this.btnDivide.Text = "/";
this.btnDivide.Click += new System.EventHandler(this.btnDivide_Click);
this.btnDivide.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
//
// btnSeven
//
this.btnSeven.BackColor = System.Drawing.Color.WhiteSmoke;
this.btnSeven.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnSeven.Location = new System.Drawing.Point(10, 19);
this.btnSeven.Name = "btnSeven";
this.btnSeven.Size = new System.Drawing.Size(20, 20);
this.btnSeven.TabIndex = 4;
this.btnSeven.TabStop = false;
this.btnSeven.Text = "7";
this.btnSeven.Click += new System.EventHandler(this.btnSeven_Click);
this.btnSeven.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
//
// btnFive
//
this.btnFive.BackColor = System.Drawing.Color.WhiteSmoke;
this.btnFive.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnFive.Location = new System.Drawing.Point(32, 41);
this.btnFive.Name = "btnFive";
this.btnFive.Size = new System.Drawing.Size(20, 20);
this.btnFive.TabIndex = 11;
this.btnFive.TabStop = false;
this.btnFive.Text = "5";
this.btnFive.Click += new System.EventHandler(this.btnFive_Click);
this.btnFive.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
//
// btnSix
//
this.btnSix.BackColor = System.Drawing.Color.WhiteSmoke;
this.btnSix.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnSix.Location = new System.Drawing.Point(54, 41);
this.btnSix.Name = "btnSix";
this.btnSix.Size = new System.Drawing.Size(20, 20);
this.btnSix.TabIndex = 10;
this.btnSix.TabStop = false;
this.btnSix.Text = "6";
this.btnSix.Click += new System.EventHandler(this.btnSix_Click);
this.btnSix.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
//
// btnMultiply
//
this.btnMultiply.BackColor = System.Drawing.Color.DarkGray;
this.btnMultiply.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnMultiply.Location = new System.Drawing.Point(76, 41);
this.btnMultiply.Name = "btnMultiply";
this.btnMultiply.Size = new System.Drawing.Size(20, 20);
this.btnMultiply.TabIndex = 9;
this.btnMultiply.TabStop = false;
this.btnMultiply.Text = "*";
this.btnMultiply.Click += new System.EventHandler(this.btnMultiply_Click);
this.btnMultiply.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
//
// btnFour
//
this.btnFour.BackColor = System.Drawing.Color.WhiteSmoke;
this.btnFour.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnFour.Location = new System.Drawing.Point(10, 41);
this.btnFour.Name = "btnFour";
this.btnFour.Size = new System.Drawing.Size(20, 20);
this.btnFour.TabIndex = 8;
this.btnFour.TabStop = false;
this.btnFour.Text = "4";
this.btnFour.Click += new System.EventHandler(this.btnFour_Click);
this.btnFour.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
//
// btnDot
//
this.btnDot.BackColor = System.Drawing.Color.WhiteSmoke;
this.btnDot.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnDot.Location = new System.Drawing.Point(32, 85);
this.btnDot.Name = "btnDot";
this.btnDot.Size = new System.Drawing.Size(20, 20);
this.btnDot.TabIndex = 19;
this.btnDot.TabStop = false;
this.btnDot.Text = ".";
this.btnDot.Click += new System.EventHandler(this.btnDot_Click);
this.btnDot.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
//
// btnPlusMinus
//
this.btnPlusMinus.BackColor = System.Drawing.Color.DarkGray;
this.btnPlusMinus.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnPlusMinus.Location = new System.Drawing.Point(54, 85);
this.btnPlusMinus.Name = "btnPlusMinus";
this.btnPlusMinus.Size = new System.Drawing.Size(20, 20);
this.btnPlusMinus.TabIndex = 18;
this.btnPlusMinus.TabStop = false;
this.btnPlusMinus.Text = "�";
this.btnPlusMinus.Click += new System.EventHandler(this.btnPlusMinus_Click);
this.btnPlusMinus.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
//
// btnPlus
//
this.btnPlus.BackColor = System.Drawing.Color.DarkGray;
this.btnPlus.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnPlus.Location = new System.Drawing.Point(76, 85);
this.btnPlus.Name = "btnPlus";
this.btnPlus.Size = new System.Drawing.Size(20, 20);
this.btnPlus.TabIndex = 17;
this.btnPlus.TabStop = false;
this.btnPlus.Text = "+";
this.btnPlus.Click += new System.EventHandler(this.btnPlus_Click);
this.btnPlus.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
//
// btnZero
//
this.btnZero.BackColor = System.Drawing.Color.WhiteSmoke;
this.btnZero.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnZero.Location = new System.Drawing.Point(10, 85);
this.btnZero.Name = "btnZero";
this.btnZero.Size = new System.Drawing.Size(20, 20);
this.btnZero.TabIndex = 16;
this.btnZero.TabStop = false;
this.btnZero.Text = "0";
this.btnZero.Click += new System.EventHandler(this.btnZero_Click);
this.btnZero.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
//
// btnTwo
//
this.btnTwo.BackColor = System.Drawing.Color.WhiteSmoke;
this.btnTwo.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnTwo.Location = new System.Drawing.Point(32, 63);
this.btnTwo.Name = "btnTwo";
this.btnTwo.Size = new System.Drawing.Size(20, 20);
this.btnTwo.TabIndex = 15;
this.btnTwo.TabStop = false;
this.btnTwo.Text = "2";
this.btnTwo.Click += new System.EventHandler(this.btnTwo_Click);
this.btnTwo.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
//
// btnThree
//
this.btnThree.BackColor = System.Drawing.Color.WhiteSmoke;
this.btnThree.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnThree.Location = new System.Drawing.Point(54, 63);
this.btnThree.Name = "btnThree";
this.btnThree.Size = new System.Drawing.Size(20, 20);
this.btnThree.TabIndex = 14;
this.btnThree.TabStop = false;
this.btnThree.Text = "3";
this.btnThree.Click += new System.EventHandler(this.btnThree_Click);
this.btnThree.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
//
// btnMinus
//
this.btnMinus.BackColor = System.Drawing.Color.DarkGray;
this.btnMinus.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnMinus.Location = new System.Drawing.Point(76, 63);
this.btnMinus.Name = "btnMinus";
this.btnMinus.Size = new System.Drawing.Size(20, 20);
this.btnMinus.TabIndex = 13;
this.btnMinus.TabStop = false;
this.btnMinus.Text = "-";
this.btnMinus.Click += new System.EventHandler(this.btnMinus_Click);
this.btnMinus.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
//
// btnOne
//
this.btnOne.BackColor = System.Drawing.Color.WhiteSmoke;
this.btnOne.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnOne.Location = new System.Drawing.Point(10, 63);
this.btnOne.Name = "btnOne";
this.btnOne.Size = new System.Drawing.Size(20, 20);
this.btnOne.TabIndex = 12;
this.btnOne.TabStop = false;
this.btnOne.Text = "1";
this.btnOne.Click += new System.EventHandler(this.btnOne_Click);
this.btnOne.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
//
// btnBack
//
this.btnBack.BackColor = System.Drawing.Color.OliveDrab;
this.btnBack.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnBack.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.btnBack.Location = new System.Drawing.Point(32, 107);
this.btnBack.Name = "btnBack";
this.btnBack.Size = new System.Drawing.Size(20, 20);
this.btnBack.TabIndex = 22;
this.btnBack.TabStop = false;
this.btnBack.Text = "<";
this.btnBack.Click += new System.EventHandler(this.btnBack_Click);
this.btnBack.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
//
// btnEqual
//
this.btnEqual.BackColor = System.Drawing.Color.DarkGray;
this.btnEqual.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnEqual.Location = new System.Drawing.Point(54, 107);
this.btnEqual.Name = "btnEqual";
this.btnEqual.Size = new System.Drawing.Size(42, 20);
this.btnEqual.TabIndex = 21;
this.btnEqual.TabStop = false;
this.btnEqual.Text = "=";
this.btnEqual.Click += new System.EventHandler(this.btnEqual_Click);
this.btnEqual.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
//
// btnClear
//
this.btnClear.BackColor = System.Drawing.Color.OliveDrab;
this.btnClear.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnClear.Location = new System.Drawing.Point(10, 107);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(20, 20);
this.btnClear.TabIndex = 20;
this.btnClear.TabStop = false;
this.btnClear.Text = "C";
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
this.btnClear.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
//
// frmCalculator
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.Control;
this.ClientSize = new System.Drawing.Size(104, 136);
this.Controls.Add(this.btnBack);
this.Controls.Add(this.btnEqual);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btnDot);
this.Controls.Add(this.btnPlusMinus);
this.Controls.Add(this.btnPlus);
this.Controls.Add(this.btnZero);
this.Controls.Add(this.btnTwo);
this.Controls.Add(this.btnThree);
this.Controls.Add(this.btnMinus);
this.Controls.Add(this.btnOne);
this.Controls.Add(this.btnFive);
this.Controls.Add(this.btnSix);
this.Controls.Add(this.btnMultiply);
this.Controls.Add(this.btnFour);
this.Controls.Add(this.btnEight);
this.Controls.Add(this.btnNine);
this.Controls.Add(this.btnDivide);
this.Controls.Add(this.btnSeven);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "frmCalculator";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "frmCalculator";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmCalculator_KeyDown);
this.Load += new System.EventHandler(this.frmCalculator_Load);
this.Leave += new System.EventHandler(this.CheckToHide);
this.Deactivate += new System.EventHandler(this.CheckToHide);
this.ResumeLayout(false);
}
#endregion
protected override void OnResize(EventArgs e)
{
if(this.Height!=136)
this.Height=136;
if(this.Width!=104)
this.Width=104;
base.OnResize (e);
}
private void CheckToHide(object sender, EventArgs e)
{
startedNew=false;
firstValue=0;
this.Hide();
}
private void frmCalculator_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyData==Keys.Escape)
{
startedNew=false;
firstValue=0;
Value=previousValue;
this.Hide();
}
switch(e.KeyData)
{
case Keys.D0:
case Keys.NumPad0:
OnNumberKeyPressed('0');
break;
case Keys.D1:
case Keys.NumPad1:
OnNumberKeyPressed('1');
break;
case Keys.D2:
case Keys.NumPad2:
OnNumberKeyPressed('2');
break;
case Keys.D3:
case Keys.NumPad3:
OnNumberKeyPressed('3');
break;
case Keys.D4:
case Keys.NumPad4:
OnNumberKeyPressed('4');
break;
case Keys.D5:
case Keys.NumPad5:
OnNumberKeyPressed('5');
break;
case Keys.D6:
case Keys.NumPad6:
OnNumberKeyPressed('6');
break;
case Keys.D7:
case Keys.NumPad7:
OnNumberKeyPressed('7');
break;
case Keys.D8:
case Keys.NumPad8:
OnNumberKeyPressed('8');
break;
case Keys.D9:
case Keys.NumPad9:
OnNumberKeyPressed('9');
break;
case Keys.Decimal:
OnNumberKeyPressed(Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));
break;
case Keys.Enter:
btnEqual_Click(this,new EventArgs());
break;
case Keys.Back:
OnNumberKeyPressed(Convert.ToChar(8));
break;
case Keys.Add:
OperationKeyPressed("+");
break;
case Keys.Divide:
OperationKeyPressed("/");
break;
case Keys.Multiply:
OperationKeyPressed("*");
break;
case Keys.Subtract:
OperationKeyPressed("-");
break;
}
}
private void btnSeven_Click(object sender, System.EventArgs e)
{
OnNumberKeyPressed('7');
}
private void btnEight_Click(object sender, System.EventArgs e)
{
OnNumberKeyPressed('8');
}
private void btnNine_Click(object sender, System.EventArgs e)
{
OnNumberKeyPressed('9');
}
private bool startedNew=false;
private void OnNumberKeyPressed(char key)
{
if(startedNew==true)
{
if(key==Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator))
{
Value=0;
NumberKeyPressed(this,new KeyPressEventArgs(key));
startedNew=false;
}
else
{
Value=double.Parse(key.ToString());
startedNew=false;
}
}
else
if(NumberKeyPressed!=null)
NumberKeyPressed(this,new KeyPressEventArgs(key));
}
private void btnDivide_Click(object sender, System.EventArgs e)
{
OperationKeyPressed("/");
}
private void OperationKeyPressed(string key)
{
if(startedNew==true)
clickedOperator=key;
PerformOperation();
clickedOperator=key;
}
private void PerformOperation()
{
if(clickedOperator!="")
{
switch(clickedOperator)
{
case "/":
if(Value!=0)
Value=firstValue/Value;
else if(ErrorOccured!=null)
{
ErrorOccured(this,"Error");
}
break;
case "*":
Value=firstValue*Value;
break;
case "-":
Value=firstValue-Value;
break;
case "+":
Value=firstValue+Value;
break;
}
}
startedNew=true;
firstValue=Value;
}
private void btnFour_Click(object sender, System.EventArgs e)
{
OnNumberKeyPressed('4');
}
private void btnFive_Click(object sender, System.EventArgs e)
{
OnNumberKeyPressed('5');
}
private void btnSix_Click(object sender, System.EventArgs e)
{
OnNumberKeyPressed('6');
}
private void btnMultiply_Click(object sender, System.EventArgs e)
{
OperationKeyPressed("*");
}
private void btnOne_Click(object sender, System.EventArgs e)
{
OnNumberKeyPressed('1');
}
private void btnTwo_Click(object sender, System.EventArgs e)
{
OnNumberKeyPressed('2');
}
private void btnThree_Click(object sender, System.EventArgs e)
{
OnNumberKeyPressed('3');
}
private void btnMinus_Click(object sender, System.EventArgs e)
{
OperationKeyPressed("-");
}
private void btnZero_Click(object sender, System.EventArgs e)
{
OnNumberKeyPressed('0');
}
private void btnDot_Click(object sender, System.EventArgs e)
{
OnNumberKeyPressed(Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));
}
private void btnPlusMinus_Click(object sender, System.EventArgs e)
{
Value=-Value;
}
private void btnPlus_Click(object sender, System.EventArgs e)
{
OperationKeyPressed("+");
}
private void btnClear_Click(object sender, System.EventArgs e)
{
clickedOperator="";
Value=0;
startedNew=true;
}
public void Show(string clickedOperator)
{
if(("+-*/").IndexOf(clickedOperator)>=0&&clickedOperator.Length==1)
{
startedNew=true;
firstValue=Value;
this.clickedOperator=clickedOperator;
}
this.Show();
}
private void btnBack_Click(object sender, System.EventArgs e)
{
if(NumberKeyPressed!=null)
NumberKeyPressed(this,new KeyPressEventArgs(Convert.ToChar(8)));
}
private void btnEqual_Click(object sender, System.EventArgs e)
{
PerformOperation();
clickedOperator="";
startedNew=false;
firstValue=0;
this.Hide();
}
private double calcValue=0;
private void frmCalculator_Load(object sender, System.EventArgs e)
{
previousValue=Value;
}
public double Value
{
get
{
return calcValue;
}
set
{
if(calcValue!=value&&ValueChanged!=null)
ValueChanged(this,value);
calcValue=value;
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
System.Drawing.Pen pen=new System.Drawing.Pen(System.Drawing.Color.Gray,1);
e.Graphics.DrawRectangle(pen,2,2,this.Width-4,this.Height-4);
e.Graphics.DrawLine(pen,2,10,this.Width-3,10);
}
}
}