ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

C#: Calculator

calculatorInCSharpfig.3

Today, I will teach you how to create a calculator using C#.Net. This tutorial contains a step by step procedure that you need to follow orderly with ease. This is a simple calculator with functions that has been done accurately.

 

To start with:

 

Open Microsoft Visual Studio 2008 and create new Windows Form Application for C#. Then do the following design of a Form just like this.

calculatorInCSharpfig.1

After that, go to the Solution Explorer, double click the “View Code”  to display the code editor.

calculatorInCSharpfig.2

In the code editor, declare all the variables that are needed.

double tot = 0;
string LogicalOperator;

do the following codes for function of the buttons (0-9 and .) .

private void btn1_Click(object sender, EventArgs e)
 {
 if (txtView.Text == "0")
 {
 txtView.Clear();
 txtView.Text += btn1.Text;
 }
 else
 {
 txtView.Text += btn1.Text;
 }
 }

 private void btn2_Click(object sender, EventArgs e)
 {
 if (txtView.Text == "0")
 {
 txtView.Clear();
 txtView.Text += btn2.Text;
 }
 else
 {
 txtView.Text += btn2.Text;
 }
 }

 private void btn3_Click(object sender, EventArgs e)
 {
 if (txtView.Text == "0")
 {
 txtView.Clear();
 txtView.Text += btn3.Text;
 }
 else
 {
 txtView.Text += btn3.Text;
 } 
 }

 private void btn0_Click(object sender, EventArgs e)
 {
 if (txtView.Text == "0")
 {
 txtView.Clear();
 txtView.Text += btn0.Text;
 }
 else
 {
 txtView.Text += btn0.Text;
 } 
 }

 private void btn8_Click(object sender, EventArgs e)
 {
 if (txtView.Text == "0")
 {
 txtView.Clear();
 txtView.Text += btn8.Text;
 }
 else
 {
 txtView.Text += btn8.Text;
 } 
 }

 private void btn7_Click(object sender, EventArgs e)
 {
 if (txtView.Text == "0")
 {
 txtView.Clear();
 txtView.Text += btn7.Text;
 }
 else
 {
 txtView.Text += btn7.Text;
 }
 
 }

 private void btn9_Click(object sender, EventArgs e)
 {
 if (txtView.Text == "0")
 {
 txtView.Clear();
 txtView.Text += btn9.Text;
 }
 else
 {
 txtView.Text += btn9.Text;
 }
 
 }

 private void btn6_Click(object sender, EventArgs e)
 {
 if (txtView.Text == "0")
 {
 txtView.Clear();
 txtView.Text += btn6.Text;
 }
 else
 {
 txtView.Text += btn6.Text;
 }
 
 }

 private void btn5_Click(object sender, EventArgs e)
 { 
 if (txtView.Text == "0")
 {
 txtView.Clear();
 txtView.Text += btn5.Text;
 }
 else
 {
 txtView.Text += btn5.Text;
 } 
 }

 private void btn4_Click(object sender, EventArgs e)
 {
 if (txtView.Text == "0")
 {
 txtView.Clear();
 txtView.Text += btn4.Text;
 }
 else
 {
 txtView.Text += btn4.Text;
 }
 }

 private void btnDote_Click(object sender, EventArgs e)
 {
 if (!txtView.Text.Contains("."))
 {
 txtView.Text += btnDote.Text;
 }
 
 }

After that, do the following codes for function of the MDAS(x,/,+,-).

 private void btnPlus_Click(object sender, EventArgs e)
 {
 tot += double.Parse(txtView.Text);
 LogicalOperator = "plus";
 txtView.Clear();


 }
 private void btnminus_Click(object sender, EventArgs e)
 {
 tot += double.Parse(txtView.Text);
 LogicalOperator = "minus";
 txtView.Clear();

 }
 private void btnMultiply_Click(object sender, EventArgs e)
 {
 tot += double.Parse(txtView.Text);
 LogicalOperator = "multiply";
 txtView.Clear();
 }

 private void btnDivide_Click(object sender, EventArgs e)
 {
 tot += double.Parse(txtView.Text);
 LogicalOperator = "divide";
 txtView.Clear();
 }

 

Do the following codes for the “Clear” button.

 private void btnClear_Click(object sender, EventArgs e)
 {
 txtView.Text = "0";
 tot = 0;
 
 }

Finally, do the following codes for the function of the equals(=) sign.

 private void btnEqual_Click(object sender, EventArgs e)
 {

 switch (LogicalOperator){
 case "plus":
 tot = tot + double.Parse(txtView.Text);
 txtView.Text = tot.ToString();
 tot = 0;
 LogicalOperator = "";
 break ;
 case "minus":
 tot = tot - double.Parse(txtView.Text);
 txtView.Text = tot.ToString();
 tot = 0;
 LogicalOperator = ""; 
 break;

 case "multiply":
 tot = tot * double.Parse(txtView.Text);
 txtView.Text = tot.ToString();
 tot = 0;
 LogicalOperator = "";
 break;
 case "divide":
 tot = tot / double.Parse(txtView.Text);
 txtView.Text = tot.ToString();
 tot = 0;
 LogicalOperator = "";
 break;
 default :
 txtView.Text = tot.ToString(); 
 break;

 }

 

For all students who need programmer for your thesis system or anyone who needs a sourcecode in any programming languages. You can contact me @ :

 

 

Email – jannopalacios@gmail.com
Mobile No. – 09305235027 – tnt

 

Download Sourcecode

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!