You are on page 1of 4

using System;

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

namespace Calculator
{
public partial class Form1 : Form
{
private String calculatedNumber; // Stores numbers in string form
private Boolean dot = false; // Flag: to control when a dot can be placed
in a number
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
LblDisplay.Focus();
}

private void BtnZero_Click(object sender, EventArgs e) //adds 0 to string


{
calculatedNumber += "0";
LblDisplay.Text=calculatedNumber;
}

private void BtnOne_Click(object sender, EventArgs e) //adds 1 to string


{
calculatedNumber += "1";
LblDisplay.Text = calculatedNumber;
}

private void BtnTwo_Click(object sender, EventArgs e) //adds 2 to string


{
calculatedNumber += "2";
LblDisplay.Text = calculatedNumber;
}

private void BtnThree_Click(object sender, EventArgs e) //adds 3 to string


{
calculatedNumber += "3";
LblDisplay.Text = calculatedNumber;
}

private void BtnFour_Click(object sender, EventArgs e) //adds 4 to string


{
calculatedNumber += "4";
LblDisplay.Text = calculatedNumber;
}
private void BtnFive_Click(object sender, EventArgs e) //adds 5 to string
{
calculatedNumber += "5";
LblDisplay.Text = calculatedNumber;
}
private void BtnSix_Click(object sender, EventArgs e) //adds 6 to string
{
calculatedNumber += "6";
LblDisplay.Text = calculatedNumber;
}

private void BtnSeven_Click(object sender, EventArgs e) //adds 7 to string


{
calculatedNumber += "7";
LblDisplay.Text = calculatedNumber;
}

private void BtnEight_Click(object sender, EventArgs e) //adds 8 to string


{
calculatedNumber += "8";
LblDisplay.Text = calculatedNumber;
}

private void BtnNine_Click(object sender, EventArgs e) //adds 9 to string


{
calculatedNumber += "9";
LblDisplay.Text = calculatedNumber;
}

private void BtnDot_Click(object sender, EventArgs e) //adds . to string


{
if (!dot)
{
calculatedNumber += ".";
dot = true;
}

LblDisplay.Text = calculatedNumber;
}

private void BtnAdd_Click(object sender, EventArgs e)


{
switch(operation)
{
case "+":
calculatedNumber = (LblDisplay.Text + Double.Parse(calculatedNumber)).ToString();
break; //addition button
}

private void BtnSub_Click(object sender, EventArgs e)


{
case "-":
calculatedNumber = (LblDisplay.Text - Double.Parse(calculatedNumber)).ToString();
break;//subtraction button
}

private void BtnDiv_Click(object sender, EventArgs e)


{
case "/":
calculatedNumber = (LblDisplay.Text / Double.Parse(calculatedNumber)).ToString();
break;//divide button
}
private void Button15_Click(object sender, EventArgs e)
{
case "*":
calculatedNumber = (LblDisplay.Text * Double.Parse(calculatedNumber)).ToString();
break; //multiply button
default:
break;
}
}

private void BtnEqt_Click(object sender, EventArgs e)


{

case "=":
calculatedNumber = (LblDisplay.Text = Double.Parse(calculatedNumber)).ToString();
break; //equals button
}

private void BtnClear_Click(object sender, EventArgs e) //resets the string to


blank
{
calculatedNumber = "";
LblDisplay.Text = calculatedNumber;
dot = false;
}

private void BtnBack_Click(object sender, EventArgs e)


{
// reset dot if removed. First make sure not empty
if (!LblDisplay.Text.Equals(""))
{
//if most right char is a . (dot) then reset flag
if ((LblDisplay.Text.ToString())[LblDisplay.Text.Length-1] == '.')
dot = false;
// go ahead and remove last char
LblDisplay.Text = calculatedNumber =
LblDisplay.Text.Remove(LblDisplay.Text.Length - 1);
}
}

private void Form1_keyDown(object sender, KeyEventArgs e) //keyboard inputs for


the calculator
{
//keyboard number input via the number pad or keyboard numbers
if (e.KeyCode.Equals(Keys.D0) || e.KeyCode.Equals(Keys.NumPad0))
{
BtnZero_Click(sender, e);
}
if (e.KeyCode.Equals(Keys.D1) || e.KeyCode.Equals(Keys.NumPad1))
{
BtnOne_Click(sender, e);
}
if (e.KeyCode.Equals(Keys.D2) || e.KeyCode.Equals(Keys.NumPad2))
{
BtnTwo_Click(sender, e);
}
if (e.KeyCode.Equals(Keys.D3) || e.KeyCode.Equals(Keys.NumPad3))
{
BtnThree_Click(sender, e);
}
if (e.KeyCode.Equals(Keys.D4) || e.KeyCode.Equals(Keys.NumPad4))
{
BtnFour_Click(sender, e);
}
if (e.KeyCode.Equals(Keys.D5) || e.KeyCode.Equals(Keys.NumPad5))
{
BtnFive_Click(sender, e);
}
if (e.KeyCode.Equals(Keys.D6) || e.KeyCode.Equals(Keys.NumPad6))
{
BtnSix_Click(sender, e);
}
if (e.KeyCode.Equals(Keys.D7) || e.KeyCode.Equals(Keys.NumPad7))
{
BtnSeven_Click(sender, e);
}
if (e.KeyCode.Equals(Keys.D8) || e.KeyCode.Equals(Keys.NumPad8))
{
BtnEight_Click(sender, e);
}
else if (e.KeyCode.Equals(Keys.D9) || e.KeyCode.Equals(Keys.NumPad9))
{
BtnNine_Click(sender, e);
}
}
}
}