[postlink]
http://uknow-iknow.blogspot.com/2016/09/c-calculator-simple.html[/postlink]
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
Double value = 0;
String operation = "";
bool op = false;
Double total_value = 0;
Double old_value = 0;
String old_operation = "";
public Form1()
{
InitializeComponent();
}
private void btn_Click(object sender, EventArgs e)
{
if ((txtResult.Text == "0")||(op))
txtResult.Clear();
op = false;
Button b = (Button)sender;
if (b.Text == ".")
{
if (txtResult.Text.Trim() == "")
txtResult.Text = "0";
if (txtResult.Text.Contains("."))
return;
txtResult.Text += ".";
}
else
{
txtResult.Text = txtResult.Text + b.Text;
}
if(txtResult.Text.Trim() != ".")
old_value = Double.Parse(txtResult.Text);
}
private void btnCE_Click(object sender, EventArgs e)
{
txtResult.Text = "0";
operation = "";
}
private void btnClear_Click(object sender, EventArgs e)
{
txtResult.Text = "0";
labelResult.Text = "";
value = 0;
}
private void operation_click(object sender, EventArgs e)
{
Button b = (Button)sender;
total_value = Double.Parse(txtResult.Text);
if (value != 0)
{
if (total_value == value)
{
return;
}
equal.PerformClick();
op = true;
operation = b.Text;
labelResult.Text = value + "" + operation;
}
else
{
value = Double.Parse(txtResult.Text);
op = true;
operation = b.Text;
labelResult.Text = value + "" + operation;
}
old_operation = b.Text;
}
private void btnEqu_Click(object sender, EventArgs e)
{
labelResult.Text = "";
switch (operation)
{
case "+":
txtResult.Text = (value + Double.Parse(txtResult.Text)).ToString();
break;
case "-":
txtResult.Text = (value - Double.Parse(txtResult.Text)).ToString();
break;
case "*":
txtResult.Text = (value * Double.Parse(txtResult.Text)).ToString();
break;
case "/":
txtResult.Text = (value / Double.Parse(txtResult.Text)).ToString();
break;
default:
break;
}
value = Double.Parse(txtResult.Text);
if (operation == "")
{
switch (old_operation)
{
case "+":
txtResult.Text = (value + old_value).ToString();
break;
case "-":
txtResult.Text = (value - old_value).ToString();
break;
case "*":
txtResult.Text = (value * old_value).ToString();
break;
case "/":
txtResult.Text = (value / old_value).ToString();
break;
default:
break;
}
}
operation = "";
}
private void btnDel_Click(object sender, EventArgs e)
{
if (txtResult.Text.Trim() != "0")
{
txtResult.Text = txtResult.Text.Substring(0, txtResult.Text.Length - 1);
txtResult.Focus();
}
}
private void labelResult_Click(object sender, EventArgs e)
{
}
private void btnPercent_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
txtResult.Text = "0";
}
private void FunctionBtn_Click(object sender, EventArgs e)
{
}
private void btnPercent_Click_1(object sender, EventArgs e)
{
btnEqu_Click(null, null);
txtResult.Text = (value / 100).ToString();
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void txtResult_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.')
{
e.Handled = true;
}
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
}
private void btnSqrt_Click(object sender, EventArgs e)
{
btnEqu_Click(null, null);
value = Double.Parse(txtResult.Text);
txtResult.Text = Math.Sqrt(value).ToString();
}
private void btnDivX_Click(object sender, EventArgs e)
{
btnEqu_Click(null, null);
value = Double.Parse(txtResult.Text);
txtResult.Text = (1 / value).ToString();
}
private void btnSqr_Click(object sender, EventArgs e)
{
btnEqu_Click(null, null);
value = Double.Parse(txtResult.Text);
txtResult.Text = (value * value).ToString();
}
private void btnAddSubstract_Click(object sender, EventArgs e)
{
if (txtResult.Text.StartsWith("-"))
{
//It's negative now, so strip the `-` sign to make it positive
txtResult.Text = txtResult.Text.Substring(1);
}
else if (!string.IsNullOrEmpty(txtResult.Text) && decimal.Parse(txtResult.Text) != 0)
{
//It's positive now, so prefix the value with the `-` sign to make it negative
txtResult.Text = "-" + txtResult.Text;
}
}
private void factorial_Click(object sender, EventArgs e)
{
int numberInt = int.Parse(txtResult.Text);
int result = numberInt;
for (int i = 1; i < numberInt; i++)
{
result = result * i;
}
txtResult.Text = result.ToString();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Double value = 0;
String operation = "";
bool op = false;
Double total_value = 0;
Double old_value = 0;
String old_operation = "";
public Form1()
{
InitializeComponent();
}
private void btn_Click(object sender, EventArgs e)
{
if ((txtResult.Text == "0")||(op))
txtResult.Clear();
op = false;
Button b = (Button)sender;
if (b.Text == ".")
{
if (txtResult.Text.Trim() == "")
txtResult.Text = "0";
if (txtResult.Text.Contains("."))
return;
txtResult.Text += ".";
}
else
{
txtResult.Text = txtResult.Text + b.Text;
}
if(txtResult.Text.Trim() != ".")
old_value = Double.Parse(txtResult.Text);
}
private void btnCE_Click(object sender, EventArgs e)
{
txtResult.Text = "0";
operation = "";
}
private void btnClear_Click(object sender, EventArgs e)
{
txtResult.Text = "0";
labelResult.Text = "";
value = 0;
}
private void operation_click(object sender, EventArgs e)
{
Button b = (Button)sender;
total_value = Double.Parse(txtResult.Text);
if (value != 0)
{
if (total_value == value)
{
return;
}
equal.PerformClick();
op = true;
operation = b.Text;
labelResult.Text = value + "" + operation;
}
else
{
value = Double.Parse(txtResult.Text);
op = true;
operation = b.Text;
labelResult.Text = value + "" + operation;
}
old_operation = b.Text;
}
private void btnEqu_Click(object sender, EventArgs e)
{
labelResult.Text = "";
switch (operation)
{
case "+":
txtResult.Text = (value + Double.Parse(txtResult.Text)).ToString();
break;
case "-":
txtResult.Text = (value - Double.Parse(txtResult.Text)).ToString();
break;
case "*":
txtResult.Text = (value * Double.Parse(txtResult.Text)).ToString();
break;
case "/":
txtResult.Text = (value / Double.Parse(txtResult.Text)).ToString();
break;
default:
break;
}
value = Double.Parse(txtResult.Text);
if (operation == "")
{
switch (old_operation)
{
case "+":
txtResult.Text = (value + old_value).ToString();
break;
case "-":
txtResult.Text = (value - old_value).ToString();
break;
case "*":
txtResult.Text = (value * old_value).ToString();
break;
case "/":
txtResult.Text = (value / old_value).ToString();
break;
default:
break;
}
}
operation = "";
}
private void btnDel_Click(object sender, EventArgs e)
{
if (txtResult.Text.Trim() != "0")
{
txtResult.Text = txtResult.Text.Substring(0, txtResult.Text.Length - 1);
txtResult.Focus();
}
}
private void labelResult_Click(object sender, EventArgs e)
{
}
private void btnPercent_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
txtResult.Text = "0";
}
private void FunctionBtn_Click(object sender, EventArgs e)
{
}
private void btnPercent_Click_1(object sender, EventArgs e)
{
btnEqu_Click(null, null);
txtResult.Text = (value / 100).ToString();
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void txtResult_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.')
{
e.Handled = true;
}
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
}
private void btnSqrt_Click(object sender, EventArgs e)
{
btnEqu_Click(null, null);
value = Double.Parse(txtResult.Text);
txtResult.Text = Math.Sqrt(value).ToString();
}
private void btnDivX_Click(object sender, EventArgs e)
{
btnEqu_Click(null, null);
value = Double.Parse(txtResult.Text);
txtResult.Text = (1 / value).ToString();
}
private void btnSqr_Click(object sender, EventArgs e)
{
btnEqu_Click(null, null);
value = Double.Parse(txtResult.Text);
txtResult.Text = (value * value).ToString();
}
private void btnAddSubstract_Click(object sender, EventArgs e)
{
if (txtResult.Text.StartsWith("-"))
{
//It's negative now, so strip the `-` sign to make it positive
txtResult.Text = txtResult.Text.Substring(1);
}
else if (!string.IsNullOrEmpty(txtResult.Text) && decimal.Parse(txtResult.Text) != 0)
{
//It's positive now, so prefix the value with the `-` sign to make it negative
txtResult.Text = "-" + txtResult.Text;
}
}
private void factorial_Click(object sender, EventArgs e)
{
int numberInt = int.Parse(txtResult.Text);
int result = numberInt;
for (int i = 1; i < numberInt; i++)
{
result = result * i;
}
txtResult.Text = result.ToString();
}
}
}
===========================================================