Click here to Skip to main content
12,633,519 members (31,702 online)
Click here to Skip to main content
Add your own
alternative version

Stats

357.9K views
9K downloads
31 bookmarked
Posted

A Calculator using C#

, 1 Oct 2001
Rate this:
Please Sign up or sign in to vote.
This is basic Calculator program written in C# 
<!-- Download Links --> <!-- Main HTML starts here -->

Introduction

This is basic Calculator program written in C# which makes use of the various Windows Form controls.

Basic mathematical functions like Addition, Subtraction, Multiplication and division can be performed using this Calculator.

using System;
using System.Windows.Forms;
using System.Drawing;


public class win:Form {

	Button[] b = new Button[10];
	Button bDot,bPlus,bSub,bMul,bDiv,bEqu,bClr;
	Panel panCalc;
	TextBox txtCalc;
	
	Double dblAcc;
	Double dblSec;
	bool blnClear,blnFrstOpen;
	String strOper;
	
	public win() {
	   try {
		this.Text="Calculator";
		panCalc=new Panel();
		txtCalc = new TextBox();

		txtCalc.Location = new Point(10,10);
		txtCalc.Size=new Size(150,10);
		txtCalc.ReadOnly=true;
		txtCalc.RightToLeft=RightToLeft.Yes;
		panCalc.Size=new Size(200,200);
		panCalc.BackColor=Color.Aqua;
		panCalc.Controls.Add(txtCalc);
		addButtons(panCalc);
		this.Size=new Size(200,225);
		this.Controls.Add(panCalc);
		
		dblAcc=0;
		dblSec=0;
		blnFrstOpen=true;
		blnClear=false;
		strOper=new String('=',1);
	    }
	    catch (Exception e) {
		Console.WriteLine("error ......  " + e.StackTrace);
	    }
	}
	
	private void addButtons(Panel p) {
		for (int i=0;i<=9;i++) {
			b[i]=new Button();
			b[i].Text=Convert.ToString(i);
			b[i].Size=new Size(25,25);
			b[i].BackColor=Color.White;
			b[i].Click+=new EventHandler(btn_clk);
			p.Controls.Add(b[i]);	
		}
		b[0].Location=new Point(10,160);
		b[1].Location=new Point(10,120);
		b[4].Location=new Point(10,80);
		b[7].Location=new Point(10,40);
		
		b[2].Location=new Point(50,120);
		b[5].Location=new Point(50,80);
		b[8].Location=new Point(50,40);
		
		b[3].Location=new Point(90,120);
		b[6].Location=new Point(90,80);
		b[9].Location=new Point(90,40);
		
		bDot=new Button();
		bDot.Size=new Size(25,25);
		bDot.Location=new Point(50,160);
		bDot.BackColor=Color.White;
		bDot.Text=".";
		bDot.Click+=new EventHandler(btn_clk);
		
		bPlus=new Button();
		bPlus.Size=new Size(25,25);
		bPlus.Location=new Point(130,160);
		bPlus.BackColor=Color.White;
		bPlus.Text="+";
		bPlus.Click+=new EventHandler(btn_Oper);
		
		bSub=new Button();
		bSub.Size=new Size(25,25);
		bSub.Location=new Point(130,120);
		bSub.BackColor=Color.White;
		bSub.Text="-";
		bSub.Click+=new EventHandler(btn_Oper);
		
		bMul=new Button();
		bMul.Size=new Size(25,25);
		bMul.Location=new Point(130,80);
		bMul.BackColor=Color.White;
		bMul.Text="*";
		bMul.Click+=new EventHandler(btn_Oper);
		
		bDiv=new Button();
		bDiv.Size=new Size(25,25);
		bDiv.Location=new Point(130,40);
		bDiv.BackColor=Color.White;
		bDiv.Text="/";
		bDiv.Click+=new EventHandler(btn_Oper);
		
		bEqu=new Button();
		bEqu.Size=new Size(25,25);
		bEqu.Location=new Point(90,160);
		bEqu.BackColor=Color.White;
		bEqu.Text="=";
		bEqu.Click+=new EventHandler(btn_equ);
		
		bClr=new Button();
		bClr.Size=new Size(20,45);
		bClr.Location=new Point(170,40);
		bClr.BackColor=Color.Orange;
		bClr.Text="AC";
		bClr.Click+=new EventHandler(btn_clr);

		p.Controls.Add(bDot);
		p.Controls.Add(bPlus);
		p.Controls.Add(bSub);
		p.Controls.Add(bMul);
		p.Controls.Add(bDiv);
		p.Controls.Add(bEqu);
		p.Controls.Add(bClr);
	}
	
	private void btn_clk(object obj,EventArgs ea) {
		if(blnClear)
			txtCalc.Text="";
		
		Button b3=(Button)obj;
		
		txtCalc.Text+=b3.Text;	
		
		if (txtCalc.Text==".")
			txtCalc.Text="0.";
		dblSec=Convert.ToDouble(txtCalc.Text);
		
		blnClear=false;
	}
	
	private static void Main() {
		Application.Run(new win());
	}
	
	private void btn_Oper(object obj,EventArgs ea) {
		Button tmp=(Button)obj;
		strOper=tmp.Text;
		if (blnFrstOpen)
			dblAcc=dblSec;
		else
			calc();

		blnFrstOpen=false;
		blnClear=true;
	}

	private void btn_clr(object obj,EventArgs ea) {
		clear();
	}

	private void btn_equ(object obj,EventArgs ea) {
		calc();
		
	}
	
	private void calc() {

		switch(strOper) {
		
			case "+":
				dblAcc+=dblSec;
				break;
			case "-":
				dblAcc-=dblSec;
				break;
			case "*":
				dblAcc*=dblSec;
				break;
			case "/":
				dblAcc/=dblSec;
				break;
		}
	
		strOper="=";
		blnFrstOpen=true;
		txtCalc.Text=Convert.ToString(dblAcc);
		dblSec=dblAcc;
	}
	
	private void clear() {
		dblAcc=0;
		dblSec=0;
		blnFrstOpen=true;
		txtCalc.Text="";
		txtCalc.Focus();

	}
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

S.Thangaraju
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
BugNamespace errors Pin
Member 1067292715-Mar-14 9:40
memberMember 1067292715-Mar-14 9:40 
Question[My vote of 1] no article Pin
CIDev28-Oct-13 10:12
professionalCIDev28-Oct-13 10:12 
GeneralMy vote of 1 Pin
Rafael Bailón Ruiz16-Dec-11 9:20
memberRafael Bailón Ruiz16-Dec-11 9:20 
GeneralMy vote of 1 Pin
cappzeta18-Sep-11 11:49
membercappzeta18-Sep-11 11:49 
Questionhi Pin
abhishek b patel5-Aug-11 2:09
memberabhishek b patel5-Aug-11 2:09 
AnswerRe: hi Pin
Member 1067292715-Mar-14 9:42
memberMember 1067292715-Mar-14 9:42 
GeneralMy vote of 3 Pin
masoudxxx20022-Jun-11 1:13
membermasoudxxx20022-Jun-11 1:13 
Questioncode Pin
V A P weerasinhe14-Jul-07 21:59
memberV A P weerasinhe14-Jul-07 21:59 
AnswerRe: code Pin
Member 1067292715-Mar-14 9:42
memberMember 1067292715-Mar-14 9:42 
Questionwhere is the article ? Pin
<font color=#FF8000>toxcct5-Oct-04 2:47
membertoxcct5-Oct-04 2:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 2 Oct 2001
Article Copyright 2001 by S.Thangaraju
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid