Results 1 to 2 of 2

Thread: Calculator code for Windows Application in C#

  1. #1
    Junior Editor
    Join Date
    Feb 2016
    Posts
    11

    Calculator code for Windows Application in C#

    Guys try this code for Calculator code based on Windows Application in C#

    Code:
    // Source Code starts
    
    using System;
    
    using System.Drawing;
    
    using System.Collections;
    
    using System.ComponentModel;
    
    using System.Windows.Forms;
    
    using System.Data;
    
    namespace Calculator1
    
    {
    
    public class Calculator1 : System.Windows.Forms.Form
    
    {
    
    #region Constructor
    
    public Calculator1()
    
    {
    
    InitializeComponent();
    
    }
    
    #endregion
    
    #region Global Variables
    
    private System.Windows.Forms.Button btnAdd;
    
    public System.Windows.Forms.TextBox txtResult;
    
    private System.Windows.Forms.Button btn1;
    
    private System.Windows.Forms.Button btn2;
    
    private System.Windows.Forms.Button btn3;
    
    private System.Windows.Forms.Button btn4;
    
    private System.Windows.Forms.Button btn5;
    
    private System.Windows.Forms.Button btn6;
    
    private System.Windows.Forms.Button btn7;
    
    private System.Windows.Forms.Button btn8;
    
    private System.Windows.Forms.Button btn9;
    
    private System.Windows.Forms.Button btn0;
    
    private System.Windows.Forms.Button btnSubtract;
    
    private System.Windows.Forms.Button btnMultiply;
    
    private System.Windows.Forms.Button btnDivide;
    
    private System.Windows.Forms.Button btnEquals;
    
    private System.Windows.Forms.Button btnClear;
    
    private System.Windows.Forms.Button btnNegative;
    
    private System.Windows.Forms.Button btnDecimal;
    
    private System.ComponentModel.Container components = null;
    
    private int opMain = 0; 
    
    private double mainNum1 = 0; 
    
    private double mainNum2 = 0; 
    
    private bool isSecond = false;
    
    private bool isDone = false;
    
    private bool isDecimal = false; 
    
    #endregion
    
    #region Dispose
    
    protected override void Dispose( bool disposing )
    
    {
    
    if( disposing )
    
    {
    
    if (components != null)
    
    {
    
    components.Dispose();
    
    }
    
    }
    
    base.Dispose( disposing );
    
    }
    
    #endregion 
    
    #region Windows Form Designer generated code
    
    private void InitializeComponent()
    
    {
    
    this.btn2 = new System.Windows.Forms.Button();
    
    this.btn3 = new System.Windows.Forms.Button();
    
    this.btn0 = new System.Windows.Forms.Button();
    
    this.btn1 = new System.Windows.Forms.Button();
    
    this.btn6 = new System.Windows.Forms.Button();
    
    this.btn7 = new System.Windows.Forms.Button();
    
    this.btn4 = new System.Windows.Forms.Button();
    
    this.btnDivide = new System.Windows.Forms.Button();
    
    this.btn9 = new System.Windows.Forms.Button();
    
    this.btnAdd = new System.Windows.Forms.Button();
    
    this.btnSubtract = new System.Windows.Forms.Button();
    
    this.btnDecimal = new System.Windows.Forms.Button();
    
    this.btnClear = new System.Windows.Forms.Button();
    
    this.txtResult = new System.Windows.Forms.TextBox();
    
    this.btnMultiply = new System.Windows.Forms.Button();
    
    this.btn5 = new System.Windows.Forms.Button();
    
    this.btn8 = new System.Windows.Forms.Button();
    
    this.btnEquals = new System.Windows.Forms.Button();
    
    this.btnNegative = new System.Windows.Forms.Button();
    
    this.SuspendLayout();
    
     
    
    this.btn2.ForeColor = 
    
    System.Drawing.Color.FromArgb(((System.Byte)(0)),((System.Byte)(0)),((System.Byte)(192)));
    
    this.btn2.Location = new System.Drawing.Point(48, 40);
    
    this.btn2.Name = "btn2";
    
    this.btn2.Size = new System.Drawing.Size(32, 32);
    
    this.btn2.TabIndex = 13;
    
    this.btn2.TabStop = false;
    
    this.btn2.Text = "2";
    
    this.btn2.Click += new System.EventHandler(this.btn2_Click);
    
    this.btn3.ForeColor = 
    
    System.Drawing.Color.FromArgb(((System.Byte)(0)),((System.Byte)(0)),((System.Byte)(192)));
    
    this.btn3.Location = new System.Drawing.Point(88, 40);
    
    this.btn3.Name = "btn3";
    
    this.btn3.Size = new System.Drawing.Size(32, 32);
    
    this.btn3.TabIndex = 12;
    
    this.btn3.TabStop = false;
    
    this.btn3.Text = "3";
    
    this.btn3.Click += new System.EventHandler(this.btn3_Click);
    
    this.btn0.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)),((System.Byte)(0)), 
    
    ((System.Byte)(192)));
    
    this.btn0.Location = new System.Drawing.Point(48, 160);
    
    this.btn0.Name = "btn0";
    
    this.btn0.Size = new System.Drawing.Size(32, 32);
    
    this.btn0.TabIndex = 5;
    
    this.btn0.TabStop = false;
    
    this.btn0.Text = "0";
    
    this.btn0.Click += new System.EventHandler(this.btn0_Click);
    
    this.btn1.ForeColor = 
    
    ystem.Drawing.Color.FromArgb(((System.Byte)(0)),((System.Byte)(0)),((System.Byte)(192)));
    
    this.btn1.Location = new System.Drawing.Point(8, 40);
    
    this.btn1.Name = "btn1";
    
    this.btn1.Size = new System.Drawing.Size(32, 32);
    
    this.btn1.TabIndex = 14;
    
    this.btn1.TabStop = false;
    
    this.btn1.Text = "1";
    
    this.btn1.Click += new System.EventHandler(this.btn1_Click_1);
    
    this.btn6.ForeColor = 
    
    System.Drawing.Color.FromArgb(((System.Byte)(0)),((System.Byte)(0)),((System.Byte)(192)));
    
    this.btn6.Location = new System.Drawing.Point(88, 80);
    
    this.btn6.Name = "btn6";
    
    this.btn6.Size = new System.Drawing.Size(32, 32);
    
    this.btn6.TabIndex = 9;
    
    this.btn6.TabStop = false;
    
    this.btn6.Text = "6";
    
    this.btn6.Click += new System.EventHandler(this.btn6_Click);
    
    this.btn7.ForeColor = 
    
    System.Drawing.Color.FromArgb(((System.Byte)(0)),((System.Byte)(0)),((System.Byte)(192)));
    
    this.btn7.Location = new System.Drawing.Point(8, 120);
    
    this.btn7.Name = "btn7";
    
    this.btn7.Size = new System.Drawing.Size(32, 32);
    
    this.btn7.TabIndex = 8;
    
    this.btn7.TabStop = false;
    
    this.btn7.Text = "7";
    
    this.btn7.Click += new System.EventHandler(this.btn7_Click);
    
    this.btn4.ForeColor = 
    
    System.Drawing.Color.FromArgb(((System.Byte)(0)),((System.Byte)(0)),((System.Byte)(192)));
    
    this.btn4.Location = new System.Drawing.Point(8, 80);
    
    this.btn4.Name = "btn4";
    
    this.btn4.Size = new System.Drawing.Size(32, 32);
    
    this.btn4.TabIndex = 11;
    
    this.btn4.TabStop = false;
    
    this.btn4.Text = "4";
    
    this.btnDivide.Location = new System.Drawing.Point(136, 160);
    
    this.btnDivide.Name = "btnDivide";
    
    this.btnDivide.Size = new System.Drawing.Size(32, 32);
    
    this.btnDivide.TabIndex = 2;
    
    this.btnDivide.TabStop = false;
    
    this.btnDivide.Text = "/";
    
    this.btnDivide.Click += new System.EventHandler(this.btnDivide_Click);
    
    this.btn9.ForeColor = 
    
    System.Drawing.Color.FromArgb(((System.Byte)(0)),((System.Byte)(0)),((System.Byte)(192)));
    
    this.btn9.Location = new System.Drawing.Point(88, 120);
    
    this.btn9.Name = "btn9";
    
    this.btn9.Size = new System.Drawing.Size(32, 32);
    
    this.btn9.TabIndex = 6;
    
    this.btn9.TabStop = false;
    
    this.btn9.Text = "9";
    
    this.btn9.Click += new System.EventHandler(this.btn9_Click);
    
    this.btnAdd.Location = new System.Drawing.Point(136, 40);
    
    this.btnAdd.Name = "btnAdd";
    
    this.btnAdd.Size = new System.Drawing.Size(32, 32);
    
    this.btnAdd.TabIndex = 15;
    
    this.btnAdd.TabStop = false;
    
    this.btnAdd.Text = "+";
    
    this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
    
    this.btnSubtract.Location = new System.Drawing.Point(136, 80);
    
    this.btnSubtract.Name = "btnSubtract";
    
    this.btnSubtract.Size = new System.Drawing.Size(32, 32);
    
    this.btnSubtract.TabIndex = 4;
    
    this.btnSubtract.TabStop = false;
    
    this.btnSubtract.Text = "-";
    
    this.btnSubtract.Click += new System.EventHandler(this.btnSubtract_Click);
    
    this.btnDecimal.Location = new System.Drawing.Point(88, 200);
    
    this.btnDecimal.Name = "btnDecimal";
    
    this.btnDecimal.Size = new System.Drawing.Size(32, 32);
    
    this.btnDecimal.TabIndex = 17;
    
    this.btnDecimal.TabStop = false;
    
    this.btnDecimal.Text = ".";
    
    this.btnDecimal.Click += new System.EventHandler(this.btnDecimal_Click);
    
    this.btnClear.ForeColor = System.Drawing.Color.Red;
    
    this.btnClear.Location = new System.Drawing.Point(8, 200);
    
    this.btnClear.Name = "btnClear";
    
    this.btnClear.Size = new System.Drawing.Size(32, 32);
    
    this.btnClear.TabIndex = 0;
    
    this.btnClear.TabStop = false;
    
    this.btnClear.Text = "C";
    
    this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
    
    this.txtResult.Location = new System.Drawing.Point(8, 8);
    
    this.txtResult.Name = "txtResult";
    
    this.txtResult.RightToLeft = System.Windows.Forms.RightToLeft.No;
    
    this.txtResult.Size = new System.Drawing.Size(160, 20);
    
    this.txtResult.TabIndex = 15;
    
    this.txtResult.TabStop = false;
    
    this.txtResult.Text = "";
    
    this.txtResult.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
    
    this.btnMultiply.Location = new System.Drawing.Point(136, 120);
    
    this.btnMultiply.Name = "btnMultiply";
    
    this.btnMultiply.Size = new System.Drawing.Size(32, 32);
    
    this.btnMultiply.TabIndex = 3;
    
    this.btnMultiply.TabStop = false;
    
    this.btnMultiply.Text = "*";
    
    this.btnMultiply.Click += new System.EventHandler(this.btnMultiply_Click);
    
    this.btn5.ForeColor = 
    
    System.Drawing.Color.FromArgb(((System.Byte)(0)),((System.Byte)(0)),((System.Byte)192)));
    
    this.btn5.Location = new System.Drawing.Point(48, 80);
    
    this.btn5.Name = "btn5";
    
    this.btn5.Size = new System.Drawing.Size(32, 32);
    
    this.btn5.TabIndex = 10;
    
    this.btn5.TabStop = false;
    
    this.btn5.Text = "5";
    
    this.btn5.Click += new System.EventHandler(this.btn5_Click);
    
    this.btn8.ForeColor = 
    
    System.Drawing.Color.FromArgb(((System.Byte)(0)),((System.Byte)(0)),((System.Byte)(192)));
    
    this.btn8.Location = new System.Drawing.Point(48, 120);
    
    this.btn8.Name = "btn8";
    
    this.btn8.Size = new System.Drawing.Size(32, 32);
    
    this.btn8.TabIndex = 7;
    
    this.btn8.TabStop = false;
    
    this.btn8.Text = "8";
    
    this.btn8.Click += new System.EventHandler(this.btn8_Click);
    
    this.btnEquals.Location = new System.Drawing.Point(136, 200);
    
    this.btnEquals.Name = "btnEquals";
    
    this.btnEquals.Size = new System.Drawing.Size(32, 32);
    
    this.btnEquals.TabIndex = 1;
    
    this.btnEquals.TabStop = false;
    
    this.btnEquals.Text = "=";
    
    this.btnEquals.Click += new System.EventHandler(this.btnEquals_Click);
    
    this.btnNegative.Location = new System.Drawing.Point(48, 200);
    
    this.btnNegative.Name = "btnNegative";
    
    this.btnNegative.Size = new System.Drawing.Size(32, 32);
    
    this.btnNegative.TabIndex = 16;
    
    this.btnNegative.TabStop = false;
    
    this.btnNegative.Text = "+/-";
    
    this.btnNegative.Click += new System.EventHandler(this.btnNegative_Click);
    
    this.AcceptButton = this.btnEquals;
    
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    
    this.ClientSize = new System.Drawing.Size(176, 245);
    
    this.Controls.AddRange(new System.Windows.Forms.Control[] 
    
    {this.btnDecimal,
    
    this.btnNegative,
    
    this.btnClear,
    
    this.btnEquals,
    
    this.btnDivide,
    
    this.btnMultiply,
    
    this.btnSubtract,
    
    this.btn0,
    
    this.btn9,
    
    this.btn8,
    
    this.btn7,
    
    this.btn6,
    
    this.btn5,
    
    this.btn4,
    
    this.btn3,
    
    this.btn2,
    
    this.btn1,
    
    this.txtResult,
    
    this.btnAdd});
    
    this.KeyPreview = true;
    
    this.Name = "Calculator1";
    
    this.Text = "Calculator";
    
    this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Btn_KeyDown);
    
    this.ResumeLayout(false);
    
    }
    
    #endregion
    
    #region setText(String textset)
    
    public void setText(String textset)
    
    {
    
    if(textset.Equals("clear"))
    
    txtResult.Text = ""; //Clear the text and reset the boolean variables.
    
    isDone = false;
    
    isSecond = false;
    
    isDecimal = false;
    
    }
    
    else
    
    {
    
    if(isSecond) //Determine if the number being entered is the begining of the second number.If it is:
    
    {
    
    txtResult.Text = textset;
    
    isSecond = false; //So Calculator knows to continue the # rather than making a new one.
    
    isDecimal = false;
    
    }
    
    else
    
    {
    
    if(isDone) //isDone lets the program know that the user just hit "=" and if they press another # to start 
    
    a new number.
    
    {
    
    txtResult.Text = textset;
    
    isDone=false; //Set isDone to false so that the number just started is added on to and a new # is not 
    
    started.
    
    }
    
    else
    
    txtResult.Text += textset; //Simply add on to the existing #.
    
    }
    
    }
    
    btnEquals.Select(); 
    
    //Set the focus back to the "=" button.
    
    }
    
    #endregion
    
    #region Calc(double num1, double num2, int op)
    
    public void Calc(double num1, double num2, int op)
    
    {
    
    double answer = 0;
    
    switch(op) {
    
    case1:| answer = num1 + num2;
    
    break;
    
    case 2:
    
    answer = num1 - num2;
    
    break;
    
    case 3:
    
    answer = num1 * num2;
    
    break;
    
    case 4:
    
    answer = num1 / num2;
    
    break;
    
    }
    
    setText(answer.ToString());}
    
    #endregion
    
    #region doEquals()
    
    private void doEquals()
    
    {
    
    mainNum2 =double.Parse(txtResult.Text);
    
    //Set the value of the second number
    
    setText("clear"); 
    
    Calc(mainNum1, mainNum2,opMain); 
    
    isDone = true; 
    
    }
    
    #endregion
    
    #region changeSign()
    
    private void changeSign()
    
    {
    
    double storNum; 
    
    if(txtResult.Text.Length > 0) 
    
    {
    
    storNum = double.Parse(txtResult.Text); 
    
    storNum *= -1; 
    
    txtResult.Text = storNum.ToString();
    
    }
    
    btnEquals.Select(); 
    
    }
    
    #endregion
    
    #region setOperator(int operation) 
    
    private void setOperator(int operation)
    
    {
    
    if(txtResult.Text.Length > 0) 
    
    {
    
    opMain = operation; 
    
    mainNum1 = double.Parse(txtResult.Text); 
    
    isSecond = true; 
    
    isDone = false; 
    
    btnEquals.Select(); 
    
    }
    
    }
    
    #endregion
    
    #region setDecimal()
    
    private void setDecimal()
    
    {
    
    if(!isDecimal) {
    
    setText(".");isDecimal = true; 
    
    }
    
    btnEquals.Select();}
    
    #endregion
    
    #region KeyBoard Events
    
    protected void Btn_KeyDown (object sender,KeyEventArgs e) 
    
    { 
    
    filterKeys(e.KeyValue);
    
    }
    
    public void filterKeys(int keyCode)
    
    {
    
    switch(keyCode)
    
    {
    
    case 96:
    
    setText("0");
    
    break;
    
    case 97:
    
    setText("1");
    
    break;
    
    case 98:
    
    setText("2");
    
    break;
    
    case 99:
    
    setText("3");
    
    break;
    
    case 100:
    
    setText("4");
    
    break;
    
    case 101:
    
    setText("5");
    
    break;
    
    case 102:
    
    setText("6");
    
    break; 
    
    case 103:
    
    setText("7");
    
    break;
    
    case 104:
    
    setText("8");
    
    break;
    
    case 105:
    
    setText("9");
    
    break;
    
    case 67:
    
    setText("clear");
    
    break;
    
    case 107:
    
    setOperator(1);
    
    break;
    
    case 109:
    
    setOperator(2);
    
    break;
    
    case 106:
    
    setOperator(3);
    
    break;
    
    case 111:
    
    setOperator(4);
    
    break;
    
    case 110:
    
    setDecimal();
    
    break;
    
    }
    
    }
    
    #endregion
    
    #region Button Functions
    
    private void btnAdd_Click(object sender,System.EventArgs e)
    
    {
    
    setOperator(1);
    
    }
    
    private void btn1_Click_1(object sender,System.EventArgs e)
    
    {
    
    setText("1");
    
    }
    
    private void btn2_Click(object sender,System.EventArgs e)
    
    {
    
    setText("2");
    
    }
    
    private void btn3_Click(object sender,System.EventArgs e)
    
    {
    
    setText("3");
    
    }
    
    private void btn4_Click(object sender,System.EventArgs e)
    
    {
    
    setText("4");
    
    }
    
    private void btn5_Click(object sender,System.EventArgs e)
    
    {
    
    setText("5");
    
    }
    
    private void btn6_Click(object sender,System.EventArgs e)
    
    {
    
    setText("6");
    
    }
    
    private void btn7_Click(object sender,System.EventArgs e)
    
    {
    
    setText("7");
    
    }
    
    private void btn8_Click(object sender,System.EventArgs e)
    
    {
    
    setText("8");
    
    }
    
    private void btn9_Click(object sender,System.EventArgs e)
    
    {
    
    setText("9");
    
    }
    
    private void btn0_Click(object sender,System.EventArgs e)
    
    {
    
    setText("0");
    
    }
    
    private void btnEquals_Click(object sender,System.EventArgs e)
    
    {
    
    doEquals();
    
    }
    
    private void btnClear_Click(object sender,System.EventArgs e)
    
    {
    
    isSecond = false;
    
    setText("clear");
    
    }
    
    private void btnSubtract_Click(object sender,System.EventArgs e)
    
    {
    
    setOperator(2);
    
    }
    
    private void btnMultiply_Click(object sender,System.EventArgs e)
    
    {
    
    setOperator(3);
    
    }
    
    private void btnDivide_Click(object sender,System.EventArgs e)
    
    {
    
    setOperator(4);
    
    }
    
    private void btnNegative_Click(object sender,System.EventArgs e)
    
    {
    
    changeSign();
    
    }
    
    private void btnDecimal_Click(object sender,System.EventArgs e)
    
    {
    
    setDecimal();
    
    }
    
    #endregion
    
    #region Main()
    
     [STAThread]
    
    static void Main()
    
    deApplication.Run(new Calculator1());
    
    }
    
    #endregion
    
    }nevermind
    
    }

  2. #2
    Junior Member
    Join Date
    Feb 2016
    Posts
    9

    Re: Calculator code for Windows Application in C#

    The calculator code for Windows Application in C# is running, do you have code of the calculator which can run in LINUX operating system. As I have a project to present the code in college as an assignment to secure of marks with good numbers.
    Help me if you have.

Similar Threads

  1. ERR_CONNECTION_RESET Error code fix
    By Islam in forum Emirates Tech Support Forum
    Replies: 1
    Last Post: 03-02-2016, 01:41 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •