C# Calculator code

Harshpinder
Posted by Harshpinder under C# category on | Points: 40 | Views : 12370
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 calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private double n1;

private double n2;

private string cal;

private bool inputstatus = true;
private void button1_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button1.Text;
}
else
{
textBox1.Text = button1.Text;
inputstatus = true;
}
}

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

private void button3_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button3.Text;
}
else
{
textBox1.Text = button3.Text;
inputstatus = true;
}
}

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

private void button5_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button5.Text;
}
else
{
textBox1.Text = button5.Text;
inputstatus = true;
}
}

private void button6_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button6.Text;
}
else
{
textBox1.Text = button6.Text;
inputstatus = true;
}
}

private void button7_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button7.Text;
}
else
{
textBox1.Text = button7.Text;
inputstatus = true;
}
}

private void button8_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button8.Text;
}
else
{
textBox1.Text = button8.Text;
inputstatus = true;
}
}

private void button9_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button9.Text;
}
else
{
textBox1.Text = button9.Text;
inputstatus = true;
}
}

private void button10_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button10.Text;
}
else
{
textBox1.Text = button10.Text;
inputstatus = true;
}
}

private void button11_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button11.Text;
}
else
{
textBox1.Text = button11.Text;
inputstatus = true;
}
}

private void button12_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length != 0)
{

n1 = System.Double.Parse(textBox1.Text);

result();

cal = "+";

}
}

private void button13_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length != 0)
{

n1 = System.Double.Parse(textBox1.Text);

result();

cal = "-";

}
}

private void button14_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length != 0)
{

n1 = System.Double.Parse(textBox1.Text);

result();

cal = "*";

}
}

private void button15_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length != 0)
{

n1 = System.Double.Parse(textBox1.Text);

result();

cal = "/";

}
}

private void button16_Click(object sender, EventArgs e)
{
result();

cal = string.Empty;

}

private void button17_Click(object sender, EventArgs e)
{
textBox1.Text = string.Empty;

n1 = 0;

n2 = 0;

cal = string.Empty;
}
private void result()
{

n2 = System.Double.Parse(textBox1.Text);

switch (cal)
{

case "+":

n1 = n1 + n2;

break;

case "-":

n1 = n1 - n2;

break;

case "*":

n1 = n1 * n2;

break;

case "/":

n1 = n1 / n2;

break;

}

textBox1.Text = n1.ToString();

inputstatus = false;

}
}
}

Comments or Responses

Posted by: T.Saravanan on: 2/21/2012 Level:Silver | Status: [Member] [MVP] | Points: 10
Nice try. But user can not easily understand your code because you not assign "name" to the button controls.

For ex :

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

In the above code you will assign the button text into textbox control. The samething is happen upto other "10" button controls. Kindly assign the name to the button controls for example...

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


Finally give a little bit explanation about your code.

Note: Kindly post your code inside the code tag.
Posted by: Harshpinder on: 2/22/2012 Level:Starter | Status: [Member] | Points: 10
yes i know i have to follow coding standards...
i apologies my mistake textbox as txt and button1 as btn1

Login to post response