Introduction
A basic Calculator program which is written in C# by using various Window Form Controls.
Calculator performs Addition, Subtraction, Division, Multiplication , Square Root, Power and find Absolute value on given input numbers.
Calculator Design View On Window Form
C# Code(On Code-Behind Window)
namespace Calculator1ByJatin
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double value = 0;
double no2 = 0;
bool OperatorPress = false;
string operationPerformed = "";
// Function Check For Point
public bool CheckForType()
{
char[] arr = txtmsg.Text.ToCharArray();
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == '.')
return true;
}
return false;
}
private void BtnAll(object sender, EventArgs e)
{
if ((txtmsg.Text == "0") || (OperatorPress))
txtmsg.Clear();
OperatorPress = false;
Button b = (Button)sender;
txtmsg.Text += b.Text;
}
private void btnCE_Click(object sender, EventArgs e)
{
txtmsg.Text = "0";
value = 0;
label2.Text = "";
}
// Single event for all alphatbets.
private void BtnAllOperator(object sender, EventArgs e)
{
if (value != 0)
{
btnequals.PerformClick();
Button b = (Button)sender;
operationPerformed = b.Text;
value = Convert.ToDouble(txtmsg.Text);
label2.Text += value + " " + operationPerformed;
OperatorPress = true;
// pow = true;
}
else
{
Button b = (Button)sender;
operationPerformed = b.Text;
value = Convert.ToDouble(txtmsg.Text);
label2.Text += value + " " + operationPerformed;
OperatorPress = true;
// pow = true;
}
}
private void btnZero_Click(object sender, EventArgs e)
{
if (txtmsg.Text != "0")
txtmsg.Text += btnZero.Text;
else
txtmsg.Text = btnZero.Text;
}
private void btnDot_Click(object sender, EventArgs e)
{
if (txtmsg.Text == "")
txtmsg.Text = "0.";
else
if (!CheckForType())
txtmsg.Text += btnDot.Text;
}
private void btnequals_Click(object sender, EventArgs e)
{
switch(operationPerformed)
{
case "+" :
txtmsg.Text= (value+Convert.ToDouble(txtmsg.Text)).ToString();
break;
case "-":
txtmsg.Text=(value-Convert.ToDouble(txtmsg.Text)).ToString();
break;
case "*":
txtmsg.Text=(value*Convert.ToDouble(txtmsg.Text)).ToString();
break;
case "/":
txtmsg.Text=(value/Convert.ToDouble(txtmsg.Text)).ToString();
break;
case "Power":
txtmsg.Text = (Math.Pow(value, Convert.ToDouble(txtmsg.Text))).ToString();
break;
}
value = Convert.ToDouble(txtmsg.Text);
label2.Text = "";
}
private void btnOff_Click_1(object sender, EventArgs e)
{
this.Close();
}
private void btnback_Click(object sender, EventArgs e)
{
txtmsg.Text = txtmsg.Text.Remove(txtmsg.Text.Length - 1);
}
private void btnsqrt_Click(object sender, EventArgs e)
{
value = double.Parse(txtmsg.Text);
no2 = Math.Sqrt(value);
txtmsg.Text = no2.ToString();
no2 = 0;
}
private void btnAbs_Click(object sender, EventArgs e)
{
value = double.Parse(txtmsg.Text);
no2 = Math.Truncate(value);
txtmsg.Text = no2.ToString();
no2 = 0;
}
double m = 0;
private void btnmplus_Click(object sender, EventArgs e)
{
m = double.Parse(txtmsg.Text);
if(txtmsg.Text!="0")
label4.Text = "M";
}
private void btnmr_Click(object sender, EventArgs e)
{
if (m > 0)
txtmsg.Text = m.ToString();
}
private void btnmc_Click(object sender, EventArgs e)
{
m = 0;
label4.Text = "";
}
}
}
0 comments:
Post a Comment