C# .Net Complete Calculator Code


C# .Net Complete Calculator Code





public Form1() {

 double total1 = 0;
 double total2 = 0;

 bool plusButtonClicked = false;
 bool minusButtonClicked = false;
 bool divideButtonClicked = false;
 bool multiplyButtonClicked = false;

 //=========================
 // NUMBER BUTTONS
 //=========================
 private void btnOne_Click(object sender, EventArgs e) {
  txtDisplay.Text = txtDisplay.Text + btnOne.Text;
 }

 private void btnTwo_Click(object sender, EventArgs e) {
  txtDisplay.Text = txtDisplay.Text + btnTwo.Text;
 }

 private void btnThree_Click(object sender, EventArgs e) {
  txtDisplay.Text = txtDisplay.Text + btnThree.Text;
 }

 //OTHER NUMBER BUTTONS HERE

 private void btnPoint_Click(object sender, EventArgs e) {
  txtDisplay.Text = txtDisplay.Text + btnPoint.Text;
 }

 //=========================================
 // MINUS, PLUS, DIVIDE, MULTIPLY BUTTONS
 //=========================================
 private void btnMinus_Click(object sender, EventArgs e) {

  total1 = total1 + double.Parse(txtDisplay.Text);
  txtDisplay.Clear();

  minusButtonClicked = true;
  plusButtonClicked = false;
  divideButtonClicked = false;
  multiplyButtonClicked = false;

 }

 private void btnPlus_Click(object sender, EventArgs e) {

  total1 = total1 + double.Parse(txtDisplay.Text);
  txtDisplay.Clear();

  plusButtonClicked = true;
  minusButtonClicked = false;
  divideButtonClicked = false;
  multiplyButtonClicked = false;
 }

        
 private void btnMultiply_Click(object sender, EventArgs e) {

  total1 = total1 + double.Parse(txtDisplay.Text);
  txtDisplay.Clear();

  minusButtonClicked = false;
  plusButtonClicked = false;
  divideButtonClicked = false;
  multiplyButtonClicked = true;
 }

 private void btnDivide_Click(object sender, EventArgs e) {

  total1 = total1 + double.Parse(txtDisplay.Text);
  txtDisplay.Clear();

  minusButtonClicked = false;
  plusButtonClicked = false;
  divideButtonClicked = true;
  multiplyButtonClicked = false;
 }

 //=========================
 // EQUALS BUTTON
 //=========================
 private void btnEquals_Click(object sender, EventArgs e) {

  if (plusButtonClicked == true) {
   total2 = total1 + double.Parse(txtDisplay.Text);
  }
  else if (minusButtonClicked == true) {
   total2 = total1 - double.Parse(txtDisplay.Text);
  }
  else if (multiplyButtonClicked == true) {
   total2 = total1 * double.Parse(txtDisplay.Text);
  }
  else if (divideButtonClicked == true) {
   total2 = total1 / double.Parse(txtDisplay.Text);
  }

  txtDisplay.Text = total2.ToString();
  total1 = 0;
 }

 private void btnClear_Click(object sender, EventArgs e) {

  plusButtonClicked = false;
  minusButtonClicked = false;
  divideButtonClicked = true;
  multiplyButtonClicked = false;

  txtDisplay.Clear();
 }

}

Comments