Code for making Calculator in C# (Windows Application)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • B D Ansari
    New Bees
    • Dec 2025
    • 6

    #1

    Code for making Calculator in C# (Windows Application)

    Be the first to make the Calculator in C# for Windows based Application


    Code:
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]// Source Code starts[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]using System;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]using System.Drawing;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]using System.Collections;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]using System.ComponentModel;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]using System.Windows.Forms;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]using System.Data;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]namespace Calculator1[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]{[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]public class Calculator1 : System.Windows.Forms.Form[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]{[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]#region Constructor[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]public Calculator1()[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]{[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]InitializeComponent();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]}[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]#endregion[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]#region Global Variables[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private System.Windows.Forms.Button btnAdd;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]public System.Windows.Forms.TextBox txtResult;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private System.Windows.Forms.Button btn1;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private System.Windows.Forms.Button btn2;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private System.Windows.Forms.Button btn3;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private System.Windows.Forms.Button btn4;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private System.Windows.Forms.Button btn5;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private System.Windows.Forms.Button btn6;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private System.Windows.Forms.Button btn7;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private System.Windows.Forms.Button btn8;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private System.Windows.Forms.Button btn9;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private System.Windows.Forms.Button btn0;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private System.Windows.Forms.Button btnSubtract;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private System.Windows.Forms.Button btnMultiply;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private System.Windows.Forms.Button btnDivide;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private System.Windows.Forms.Button btnEquals;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private System.Windows.Forms.Button btnClear;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private System.Windows.Forms.Button btnNegative;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private System.Windows.Forms.Button btnDecimal;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private System.ComponentModel.Container components = null;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private int opMain = 0; [/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private double mainNum1 = 0; [/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private double mainNum2 = 0; [/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private bool isSecond = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private bool isDone = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private bool isDecimal = false; [/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]#endregion[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]#region Dispose[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]protected override void Dispose( bool disposing )[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]{[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]if( disposing )[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]{[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]if (components != null)[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]{[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]components.Dispose();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]}[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]}[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]base.Dispose( disposing );[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]}[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]#endregion [/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]#region Windows Form Designer generated code[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]private void InitializeComponent()[/COLOR][/FONT][/FONT]
    God bless you my Son
  • B D Ansari
    New Bees
    • Dec 2025
    • 6

    #2
    Code:
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]{[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn2 = new System.Windows.Forms.Button();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn3 = new System.Windows.Forms.Button();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn0 = new System.Windows.Forms.Button();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn1 = new System.Windows.Forms.Button();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn6 = new System.Windows.Forms.Button();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn7 = new System.Windows.Forms.Button();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn4 = new System.Windows.Forms.Button();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnDivide = new System.Windows.Forms.Button();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn9 = new System.Windows.Forms.Button();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnAdd = new System.Windows.Forms.Button();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnSubtract = new System.Windows.Forms.Button();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnDecimal = new System.Windows.Forms.Button();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnClear = new System.Windows.Forms.Button();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.txtResult = new System.Windows.Forms.TextBox();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnMultiply = new System.Windows.Forms.Button();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn5 = new System.Windows.Forms.Button();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn8 = new System.Windows.Forms.Button();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnEquals = new System.Windows.Forms.Button();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnNegative = new System.Windows.Forms.Button();[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.SuspendLayout();[/COLOR][/FONT][/FONT]
    
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn2.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)),( (System.Byte)(0)),((System.Byte)(192)));[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn2.Location = new System.Drawing.Point(48, 40);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn2.Name = "btn2";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn2.Size = new System.Drawing.Size(32, 32);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn2.TabIndex = 13;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn2.TabStop = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn2.Text = "2";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn2.Click += new System.EventHandler(this.btn2_Click);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn3.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)),( (System.Byte)(0)),((System.Byte)(192)));[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn3.Location = new System.Drawing.Point(88, 40);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn3.Name = "btn3";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn3.Size = new System.Drawing.Size(32, 32);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn3.TabIndex = 12;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn3.TabStop = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn3.Text = "3";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn3.Click += new System.EventHandler(this.btn3_Click);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn0.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)),( (System.Byte)(0)), ((System.Byte)(192)));[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn0.Location = new System.Drawing.Point(48, 160);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn0.Name = "btn0";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn0.Size = new System.Drawing.Size(32, 32);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn0.TabIndex = 5;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn0.TabStop = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn0.Text = "0";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn0.Click += new System.EventHandler(this.btn0_Click);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn1.ForeColor = ystem.Drawing.Color.FromArgb(((System.Byte)(0)),(( System.Byte)(0)),((System.Byte)(192)));[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn1.Location = new System.Drawing.Point(8, 40);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn1.Name = "btn1";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn1.Size = new System.Drawing.Size(32, 32);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn1.TabIndex = 14;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn1.TabStop = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn1.Text = "1";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn1.Click += new System.EventHandler(this.btn1_Click_1);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn6.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)),( (System.Byte)(0)),((System.Byte)(192)));[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn6.Location = new System.Drawing.Point(88, 80);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn6.Name = "btn6";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn6.Size = new System.Drawing.Size(32, 32);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn6.TabIndex = 9;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn6.TabStop = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn6.Text = "6";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn6.Click += new System.EventHandler(this.btn6_Click);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn7.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)),( (System.Byte)(0)),((System.Byte)(192)));[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn7.Location = new System.Drawing.Point(8, 120);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn7.Name = "btn7";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn7.Size = new System.Drawing.Size(32, 32);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn7.TabIndex = 8;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn7.TabStop = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn7.Text = "7";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn7.Click += new System.EventHandler(this.btn7_Click);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn4.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)),( (System.Byte)(0)),((System.Byte)(192)));[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn4.Location = new System.Drawing.Point(8, 80);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn4.Name = "btn4";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn4.Size = new System.Drawing.Size(32, 32);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn4.TabIndex = 11;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn4.TabStop = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn4.Text = "4";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnDivide.Location = new System.Drawing.Point(136, 160);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnDivide.Name = "btnDivide";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnDivide.Size = new System.Drawing.Size(32, 32);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnDivide.TabIndex = 2;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnDivide.TabStop = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnDivide.Text = "/";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnDivide.Click += new System.EventHandler(this.btnDivide_Click);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn9.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)),( (System.Byte)(0)),((System.Byte)(192)));[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn9.Location = new System.Drawing.Point(88, 120);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn9.Name = "btn9";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn9.Size = new System.Drawing.Size(32, 32);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn9.TabIndex = 6;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn9.TabStop = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn9.Text = "9";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn9.Click += new System.EventHandler(this.btn9_Click);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnAdd.Location = new System.Drawing.Point(136, 40);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnAdd.Name = "btnAdd";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnAdd.Size = new System.Drawing.Size(32, 32);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnAdd.TabIndex = 15;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnAdd.TabStop = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnAdd.Text = "+";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnSubtract.Location = new System.Drawing.Point(136, 80);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnSubtract.Name = "btnSubtract";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnSubtract.Size = new System.Drawing.Size(32, 32);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnSubtract.TabIndex = 4;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnSubtract.TabStop = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnSubtract.Text = "-";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnSubtract.Click += new System.EventHandler(this.btnSubtract_Click);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnDecimal.Location = new System.Drawing.Point(88, 200);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnDecimal.Name = "btnDecimal";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnDecimal.Size = new System.Drawing.Size(32, 32);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnDecimal.TabIndex = 17;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnDecimal.TabStop = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnDecimal.Text = ".";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnDecimal.Click += new System.EventHandler(this.btnDecimal_Click);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnClear.ForeColor = System.Drawing.Color.Red;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnClear.Location = new System.Drawing.Point(8, 200);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnClear.Name = "btnClear";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnClear.Size = new System.Drawing.Size(32, 32);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnClear.TabIndex = 0;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnClear.TabStop = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnClear.Text = "C";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnClear.Click += new System.EventHandler(this.btnClear_Click);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.txtResult.Location = new System.Drawing.Point(8, 8);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.txtResult.Name = "txtResult";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.txtResult.RightToLeft = System.Windows.Forms.RightToLeft.No;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.txtResult.Size = new System.Drawing.Size(160, 20);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.txtResult.TabIndex = 15;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.txtResult.TabStop = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.txtResult.Text = "";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.txtResult.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnMultiply.Location = new System.Drawing.Point(136, 120);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnMultiply.Name = "btnMultiply";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnMultiply.Size = new System.Drawing.Size(32, 32);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnMultiply.TabIndex = 3;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnMultiply.TabStop = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnMultiply.Text = "*";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnMultiply.Click += new System.EventHandler(this.btnMultiply_Click);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn5.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)),( (System.Byte)(0)),((System.Byte)192)));[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn5.Location = new System.Drawing.Point(48, 80);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn5.Name = "btn5";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn5.Size = new System.Drawing.Size(32, 32);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn5.TabIndex = 10;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn5.TabStop = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn5.Text = "5";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn5.Click += new System.EventHandler(this.btn5_Click);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn8.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)),( (System.Byte)(0)),((System.Byte)(192)));[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn8.Location = new System.Drawing.Point(48, 120);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn8.Name = "btn8";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn8.Size = new System.Drawing.Size(32, 32);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn8.TabIndex = 7;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn8.TabStop = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn8.Text = "8";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn8.Click += new System.EventHandler(this.btn8_Click);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnEquals.Location = new System.Drawing.Point(136, 200);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnEquals.Name = "btnEquals";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnEquals.Size = new System.Drawing.Size(32, 32);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnEquals.TabIndex = 1;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnEquals.TabStop = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnEquals.Text = "=";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnEquals.Click += new System.EventHandler(this.btnEquals_Click);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnNegative.Location = new System.Drawing.Point(48, 200);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnNegative.Name = "btnNegative";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnNegative.Size = new System.Drawing.Size(32, 32);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnNegative.TabIndex = 16;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnNegative.TabStop = false;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnNegative.Text = "+/-";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnNegative.Click += new System.EventHandler(this.btnNegative_Click);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.AcceptButton = this.btnEquals;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.ClientSize = new System.Drawing.Size(176, 245);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.Controls.AddRange(new System.Windows.Forms.Control[] [/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]{this.btnDecimal,[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnNegative,[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnClear,[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnEquals,[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnDivide,[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnMultiply,[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnSubtract,[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn0,[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn9,[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn8,[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn7,[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn6,[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn5,[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn4,[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn3,[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn2,[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btn1,[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.txtResult,[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.btnAdd});[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.KeyPreview = true;[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.Name = "Calculator1";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.Text = "Calculator";[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Btn_KeyD own);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]this.ResumeLayout(false);[/COLOR][/FONT][/FONT]
    [FONT=Calibri][FONT=Courier New][COLOR=#333333]}[/COLOR][/FONT][/FONT]
    God bless you my Son

    Comment

    • B D Ansari
      New Bees
      • Dec 2025
      • 6

      #3

      #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

      }
      God bless you my Son

      Comment

      • Sura
        New Bees
        • Dec 2025
        • 9

        #4
        It is working but there is some problem in my system, when I am running the Code of Calculator on computer system it is showing properly. I think my system is rebooting, any solution for solving rebooting issue.

        Comment

        Working...