Copy and paste the following code in Form2.cs.
Form2.cs
using System;
using System.Windows.Forms;
namespace Calculator
{
public partial class Calculator : Form
{
Double resultValue = 0;
String operationPerformed = "";
bool isOperationPerformed = false;
public Calculator()
{
InitializeComponent();
}
private void button_Click(object sender, EventArgs e)
{
if ((textBox_Result.Text == "0") || (isOperationPerformed))
textBox_Result.Clear();
isOperationPerformed = false;
Button button = (Button)sender;
if(button.Text == ".")
{
if (textBox_Result.Text.Contains("."))
textBox_Result.Text = textBox_Result.Text + button.Text;
}
textBox_Result.Text = textBox_Result.Text + button.Text;
}
private void operator_click(object sender, EventArgs e)
{
Button button = (Button)sender;
if (resultValue != 0)
{
button11.PerformClick();
operationPerformed = button.Text;
labelCurrentOperation.Text = resultValue + " " + operationPerformed;
isOperationPerformed = true;
}
else
{
operationPerformed = button.Text;
resultValue = Double.Parse(textBox_Result.Text);
labelCurrentOperation.Text = resultValue + " " + operationPerformed;
isOperationPerformed = true;
}
}
private void button5_Click(object sender, EventArgs e)
{
textBox_Result.Text = "0";
resultValue = 0;
}
private void button11_Click(object sender, EventArgs e)
{
switch (operationPerformed)
{
case "+":
textBox_Result.Text = (resultValue + Double.Parse(textBox_Result.Text)).ToString();
break;
case "-":
textBox_Result.Text = (resultValue - Double.Parse(textBox_Result.Text)).ToString();
break;
case "*":
textBox_Result.Text = (resultValue * Double.Parse(textBox_Result.Text)).ToString();
break;
case "/":
textBox_Result.Text = (resultValue / Double.Parse(textBox_Result.Text)).ToString();
break;
default:
break;
}
resultValue = Double.Parse(textBox_Result.Text);
labelCurrentOperation.Text = "";
}
}
}
In the next post I will show you complete coding of Program.cs step by step.
Next:
Calculator code part 3