SoFunction
Updated on 2025-03-07

Example code for windows calculator written in C#


using System;
using ;
using ;
using ;
using ;
using ;
using ;
namespace comput
{
    /// <summary>
/// This is a simple implementation of a calculator.
    /// </summary>
    public class Form1 :
    {
#region control declaration
        private txtShow;
        private groupBox1;
        private btn_rev;
        private btn_dot;
        private btn_add;
        private btn_equ;
        private btn_sign;
        private btn_sub;
        private btn_mul;
        private btn_0;
        private btn_3;
        private btn_2;
        private btn_1;
        private btn_6;
        private btn_5;
        private btn_4;
        private btn_sqrt;
        private btn_div;
        private btn_7;
        private btn_8;
        private btn_9;
        private mainMenu1;
        private menuItem1;
        private btn_sqr;
        private menuItem2;
        private menuItem3;
        private menuItem4;
        private menuItem5;
        private menuItem6;
        private c;
        private ce;
        private menuItem8;
        private menuItem9;
        private menuItem10;
        /// <summary>
/// Required designer variables.
        /// </summary>
        private components = null;
        public Form1()
        {
            //
// Required for Windows Form Designer Support
            //
            InitializeComponent();
            //
// TODO: Add any constructor code after InitializeComponent call
            //
        }
        /// <summary>
/// Clean up all resources in use.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    ();
                }
            }
            ( disposing );
        }
       #endregion
#region Method of properties of each control Windows Form Designer generated code
        /// <summary>
/// Designer supports required methods - do not use code editor to modify
/// Contents of this method.
        /// </summary>
        private void InitializeComponent()
        {
            resources = new (typeof(Form1));
            = new ();
            this.groupBox1 = new ();
            = new ();
            = new ();
            this.btn_rev = new ();
            this.btn_dot = new ();
            this.btn_add = new ();
            this.btn_equ = new ();
            this.btn_sign = new ();
            this.btn_sub = new ();
            this.btn_mul = new ();
            this.btn_0 = new ();
            this.btn_3 = new ();
            this.btn_2 = new ();
            this.btn_1 = new ();
            this.btn_6 = new ();
            this.btn_5 = new ();
            this.btn_4 = new ();
            this.btn_sqrt = new ();
            this.btn_div = new ();
            this.btn_7 = new ();
            this.btn_8 = new ();
            this.btn_9 = new ();
            this.btn_sqr = new ();
            this.mainMenu1 = new ();
            this.menuItem1 = new ();
            this.menuItem2 = new ();
            this.menuItem3 = new ();
            this.menuItem4 = new ();
            this.menuItem5 = new ();
            this.menuItem6 = new ();
            this.menuItem8 = new ();
            this.menuItem9 = new ();
            this.menuItem10 = new ();
            this.();
            ();
            //
            // txtShow
            //
            = ((()(192)), (()(192)), (()(255)));
= new ("Song style", 10.5F, , , (()(134)));
            = ;
            = new (25, 8);
            = "txtShow";
            = new (228, 23);
            = 1;
            = "0.";
            = ;
            //
            // groupBox1
            //
            this.();
            this.();
            this.(this.btn_rev);
            this.(this.btn_dot);
            this.(this.btn_add);
            this.(this.btn_equ);
            this.(this.btn_sign);
            this.(this.btn_sub);
            this.(this.btn_mul);
            this.(this.btn_0);
            this.(this.btn_3);
            this.(this.btn_2);
            this.(this.btn_1);
            this.(this.btn_6);
            this.(this.btn_5);
            this.(this.btn_4);
            this.(this.btn_sqrt);
            this.(this.btn_div);
            this.(this.btn_7);
            this.(this.btn_8);
            this.(this.btn_9);
            this.(this.btn_sqr);
            this. = new (24, 40);
            this. = "groupBox1";
            this. = new (232, 184);
            this. = 21;
            this. = false;
this. = "Computer Area";
            //
            // c
            //
= new ("Song style", 10.5F, , , (()(134)));
            = ;
            = new (178, 48);
            = "c";
            = new (36, 61);
            = 41;
            = "C";
            += new (this.btn_Oper);
            //
            // ce
            //
= new ("Song style", 10.5F, , , (()(134)));
            = ;
            = new (138, 16);
            = "ce";
            = new (76, 29);
            = 40;
            = "CE";
            += new (this.btn_Oper);
            //
            // btn_rev
            //
this.btn_rev.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_rev.ForeColor = ;
            this.btn_rev.Location = new (178, 112);
            this.btn_rev.Name = "btn_rev";
            this.btn_rev.Size = new (36, 29);
            this.btn_rev.TabIndex = 39;
            this.btn_rev.Text = "1/x";
            this.btn_rev.Click += new (this.btn_Oper);
            //
            // btn_dot
            //
this.btn_dot.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_dot.Location = new (98, 144);
            this.btn_dot.Name = "btn_dot";
            this.btn_dot.Size = new (36, 29);
            this.btn_dot.TabIndex = 38;
            this.btn_dot.Tag = "0";
            this.btn_dot.Text = ".";
            this.btn_dot.Click += new (this.btn_Oper);
            //
            // btn_add
            //
this.btn_add.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_add.ForeColor = ;
            this.btn_add.Location = new (138, 144);
            this.btn_add.Name = "btn_add";
            this.btn_add.Size = new (36, 29);
            this.btn_add.TabIndex = 37;
            this.btn_add.Text = "+";
            this.btn_add.Click += new (this.btn_Oper);
            //
            // btn_equ
            //
this.btn_equ.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_equ.ForeColor = ;
            this.btn_equ.Location = new (178, 144);
            this.btn_equ.Name = "btn_equ";
            this.btn_equ.Size = new (36, 29);
            this.btn_equ.TabIndex = 36;
            this.btn_equ.Text = "=";
            this.btn_equ.Click += new (this.btn_equ_Click);
            //
            // btn_sign
            //
this.btn_sign.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_sign.ForeColor = ;
            this.btn_sign.Location = new (58, 144);
            this.btn_sign.Name = "btn_sign";
            this.btn_sign.Size = new (36, 29);
            this.btn_sign.TabIndex = 35;
            this.btn_sign.Text = "+/-";
            this.btn_sign.Click += new (this.btn_Oper);
            //
            // btn_sub
            //
this.btn_sub.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_sub.ForeColor = ;
            this.btn_sub.Location = new (138, 112);
            this.btn_sub.Name = "btn_sub";
            this.btn_sub.Size = new (36, 29);
            this.btn_sub.TabIndex = 34;
            this.btn_sub.Text = "-";
            this.btn_sub.Click += new (this.btn_Oper);
            //
            // btn_mul
            //
this.btn_mul.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_mul.ForeColor = ;
            this.btn_mul.Location = new (138, 80);
            this.btn_mul.Name = "btn_mul";
            this.btn_mul.Size = new (36, 29);
            this.btn_mul.TabIndex = 33;
            this.btn_mul.Text = "*";
            this.btn_mul.Click += new (this.btn_Oper);
            //
            // btn_0
            //
this.btn_0.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_0.ForeColor = ;
            this.btn_0.Location = new (18, 144);
            this.btn_0.Name = "btn_0";
            this.btn_0.Size = new (36, 29);
            this.btn_0.TabIndex = 32;
            this.btn_0.Tag = "0";
            this.btn_0.Text = "0";
            this.btn_0.Click += new (this.btn_0_Click);
            //
            // btn_3
            //
this.btn_3.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_3.ForeColor = ;
            this.btn_3.Location = new (98, 112);
            this.btn_3.Name = "btn_3";
            this.btn_3.Size = new (36, 29);
            this.btn_3.TabIndex = 31;
            this.btn_3.Tag = "3";
            this.btn_3.Text = "3";
            this.btn_3.Click += new (this.btn_0_Click);
            //
            // btn_2
            //
this.btn_2.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_2.ForeColor = ;
            this.btn_2.Location = new (58, 112);
            this.btn_2.Name = "btn_2";
            this.btn_2.Size = new (36, 29);
            this.btn_2.TabIndex = 30;
            this.btn_2.Tag = "2";
            this.btn_2.Text = "2";
            this.btn_2.Click += new (this.btn_0_Click);
            //
            // btn_1
            //
this.btn_1.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_1.ForeColor = ;
            this.btn_1.Location = new (18, 112);
            this.btn_1.Name = "btn_1";
            this.btn_1.Size = new (36, 29);
            this.btn_1.TabIndex = 29;
            this.btn_1.Tag = "1";
            this.btn_1.Text = "1";
            this.btn_1.Click += new (this.btn_0_Click);
            //
            // btn_6
            //
this.btn_6.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_6.ForeColor = ;
            this.btn_6.Location = new (98, 80);
            this.btn_6.Name = "btn_6";
            this.btn_6.Size = new (36, 29);
            this.btn_6.TabIndex = 28;
            this.btn_6.Tag = "6";
            this.btn_6.Text = "6";
            this.btn_6.Click += new (this.btn_0_Click);
            //
            // btn_5
            //
this.btn_5.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_5.ForeColor = ;
            this.btn_5.Location = new (58, 80);
            this.btn_5.Name = "btn_5";
            this.btn_5.Size = new (36, 29);
            this.btn_5.TabIndex = 27;
            this.btn_5.Tag = "5";
            this.btn_5.Text = "5";
            this.btn_5.Click += new (this.btn_0_Click);
            //
            // btn_4
            //
this.btn_4.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_4.ForeColor = ;
            this.btn_4.Location = new (18, 80);
            this.btn_4.Name = "btn_4";
            this.btn_4.Size = new (36, 29);
            this.btn_4.TabIndex = 26;
            this.btn_4.Tag = "4";
            this.btn_4.Text = "4";
            this.btn_4.Click += new (this.btn_0_Click);
            //
            // btn_sqrt
            //
this.btn_sqrt.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_sqrt.ForeColor = ;
            this.btn_sqrt.Location = new (18, 16);
            this.btn_sqrt.Name = "btn_sqrt";
            this.btn_sqrt.Size = new (76, 29);
            this.btn_sqrt.TabIndex = 25;
            this.btn_sqrt.Text = "sqrt";
            this.btn_sqrt.Click += new (this.btn_Oper);
            //
            // btn_div
            //
this.btn_div.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_div.ForeColor = ;
            this.btn_div.Location = new (138, 48);
            this.btn_div.Name = "btn_div";
            this.btn_div.Size = new (36, 29);
            this.btn_div.TabIndex = 24;
            this.btn_div.Text = "/";
            this.btn_div.Click += new (this.btn_Oper);
            //
            // btn_7
            //
this.btn_7.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_7.ForeColor = ;
            this.btn_7.Location = new (18, 48);
            this.btn_7.Name = "btn_7";
            this.btn_7.Size = new (36, 29);
            this.btn_7.TabIndex = 23;
            this.btn_7.Tag = "7";
            this.btn_7.Text = "7";
            this.btn_7.Click += new (this.btn_0_Click);
            //
            // btn_8
            //
this.btn_8.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_8.ForeColor = ;
            this.btn_8.Location = new (58, 48);
            this.btn_8.Name = "btn_8";
            this.btn_8.Size = new (36, 29);
            this.btn_8.TabIndex = 22;
            this.btn_8.Tag = "8";
            this.btn_8.Text = "8";
            this.btn_8.Click += new (this.btn_0_Click);
            //
            // btn_9
            //
this.btn_9.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_9.ForeColor = ;
            this.btn_9.Location = new (98, 48);
            this.btn_9.Name = "btn_9";
            this.btn_9.Size = new (36, 29);
            this.btn_9.TabIndex = 21;
            this.btn_9.Tag = "9";
            this.btn_9.Text = "9";
            this.btn_9.Click += new (this.btn_0_Click);
            //
            // btn_sqr
            //
this.btn_sqr.Font = new ("Song style", 10.5F, , , (()(134)));
            this.btn_sqr.ForeColor = ;
            this.btn_sqr.Location = new (98, 16);
            this.btn_sqr.Name = "btn_sqr";
            this.btn_sqr.Size = new (36, 29);
            this.btn_sqr.TabIndex = 19;
            this.btn_sqr.Text = "sqr";
            this.btn_sqr.Click += new (this.btn_Oper);
            //
            // mainMenu1
            //
            this.(new [] {
                                                                                      this.menuItem8,
                                                                                      this.menuItem1,
                                                                                      this.menuItem4});
            //
            // menuItem1
            //
            this. = 1;
            this.(new [] {
                                                                                      this.menuItem2,
                                                                                      this.menuItem3});
this. = "Edit(&E)";
            //
            // menuItem2
            //
            this. = 0;
this. = "&C";
            this. += new (this.menuItem2_Click);
            //
            // menuItem3
            //
            this. = 1;
this. = "&Press (&P)";
            this. += new (this.menuItem3_Click);
            //
            // menuItem4
            //
            this. = 2;
            this.(new [] {
                                                                                      this.menuItem5,
                                                                                      this.menuItem6});
this. = "Help(&H)";
            //
            // menuItem5
            //
            this. = 0;
this. = "Help Topic(&H)";
            this. += new (this.menuItem5_Click);
            //
            // menuItem6
            //
            this. = 1;
this. = "About (&A)";
            this. += new (this.menuItem6_Click);
            //
            // menuItem8
            //
            this. = 0;
            this.(new [] {
                                                                                      this.menuItem9,
                                                                                      this.menuItem10});
this. = "&File";
            //
            // menuItem9
            //
            this. = 0;
This. = "Open windows calculator (&O)";
            this. += new (this.menuItem9_Click);
            //
            // menuItem10
            //
            this. = 1;
this. = "Exit(&Q)";
            this. += new (this.menuItem10_Click);
            //
            // Form1
            //
            = new (6, 14);
            = new (278, 239);
            (this.groupBox1);
            ();
            = .Fixed3D;
            = (()(("$")));
            = false;
            = this.mainMenu1;
            = "Form1";
            = ;
= "Calculator";
            this.(false);
            (false);
        }
        #endregion
#region Declaration of each variable and constant
public const int NULL = 0;       // Define the operation code
public const int ADD = 1;     // means addition
public const int SUB = 2;     //Decrease
public const int MUL = 3;     //Multiple
public const int DIV = 4;    //Except
public const int SQR = 5;    //Square
public const int SQRT = 6;   //Find the square root
public const int NODOT  = 0;     // Define whether the decimal point is clicked, 0 is no point
        public const int HASDOT = 1;
private double res = 0;            // Record the number of results
private double tmp = 0;             // The current input operand
private int opt ​​= NULL;           // Record the operation code
private int dot = NODOT;     // Record whether the decimal point is clicked, 0 means no point
private int num = 0;           // Record the number of input operands
private int dotnum = 0;          // Record the number of decimal points
string strOper;
        #endregion
        /// <summary>
/// The main entry point of the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            (new Form1());
        }
        /// <summary>
#region Get operand event
//Get operand event
        private void btn_0_Click(object sender, e)
        {
            btnTmp;
            double i;
            btnTmp = sender as ;           
            if (btnTmp != null)
            {
                if (dot == NODOT)
                {
// No decimal point was clicked
i = (()); //Take the user-defined control association number and convert it into double type
                    tmp = tmp * 10 + i;
= ();   //Put it into the text display
                }
else                else   // Clicked on the decimal point
                {                   
dotnum++;     //Record the number of decimal points
// Generate new value for the decimal part
                    i = (()) / (10,dotnum);
tmp = tmp + i; //Add the value after the decimal point to the current operation number
                    = ();
                }
            }
        }
        #endregion
#region equals event sum equals operation method
//Equal to event
        private void btn_equ_Click(object sender, e)
        {
            calc();
        }
//Equal to the calculation method
        private void calc()
        {
// Generate results
if (num == 0 )     //If there is an operand, if there is no, return 0
            {
                res = 0;
                tmp = 0;
                = ();
                return;
            }
switch (opt) //Find the corresponding operator for calculation
            {
// Addition
                case ADD:
                    res = res + tmp;                   
                    break;
// Subtraction
                case SUB:
                    res = res - tmp;                   
                    break;
// Multiplication
                case MUL:
                    res = res * tmp;                   
                    break;
// Division
                case DIV:
                    res = res / tmp;                   
                    break;
// Square
                case SQR:
                    res = tmp * tmp;                   
                    break;
// Square root
                case SQRT:
                    res = (tmp);                   
                    break;
                default:
                    return;
            }
= ();   //The result is output to the text display
opt = NULL; //Operator clear
            tmp=0;   
            dot = NODOT;
                    //res = 0;
                    //num = 0;
        }
        #endregion
#region Get operator transport event
//Get operator transport event
        private void btn_Oper(object obj,EventArgs ea)
        {
            Button tmp1=(Button)obj;
            strOper=;
            switch (strOper)
            {
case "/":
                    if(opt!=NULL&&opt!=DIV)
                    {
                        calc();
                    }
                    opt = DIV;
if (num != 0) //Judge the number of operands, if two, do binary operations
                    {
                        if (tmp != 0)
                            res = res / tmp;
                    }
                    else
                        res = tmp;
                    num++;                     
                    tmp = 0;
                    = ();
                    dot = NODOT;
                    break;
                case "*":
// Multiplication operation
                    if(opt!=NULL&&opt!=MUL)
                    {
                        calc();
                    }
                    opt = MUL;
if (num!= 0 )                                                                                                                          �
                    {
                            if(tmp!=0)
                         res = res * tmp;
                    }
                    else
                        res = tmp;
                    num++;                                     
                    tmp = 0;
                    = ();
                    dot = NODOT;
                    break;
case "+":
                    if(opt!=NULL&&opt!=ADD)
                    {
                        calc();
                    }
                    opt = ADD;
if (num != 0) //Judge the number of operands, if two, do binary operations
                        res = res + tmp;
                    else
                        res = tmp;
                    num++;                   
                    tmp = 0;
                    = ();
                    dot = NODOT;
                    break;
case "-": //Subtraction operation
                    if(opt!=NULL&&opt!=SUB)
                    {
                        calc();
                    }
                    /*if(opt==ADD)
                    {
                        res=res+tmp;
                        tmp=0;
                    }*/
                    opt = SUB;
if (num != 0) //Judge the number of operands, if two, do binary operations
                        res = res - tmp;
                    else
                        res = tmp;
                    num++;                    
                    tmp = 0;
                    = ();
                    dot = NODOT;
                    break;
case "sqrt":     //Square Root Operation
                    if(opt!=NULL)
                    {
                        calc();
                    }
                    //opt=SQRT;
if (tmp > 0) //Require operands to be greater than 0
                    {
                        res = (tmp);
                        //res=tmp;
                    }
                    else if(res>0)
                        res= (res);
                        =();
                    num++;   
                    tmp=0;
                    dot = NODOT;
                    break;
                case "sqr":
// Square operation
                    if(opt!=NULL)
                    {
                        calc();
                    }
                    //opt=SQR;
                    if (tmp != 0)
                    {
                        res = tmp * tmp;
                        //res=tmp;
                    }
                    else
                        res=res*res;
                    = ();
                    num++;   
                    tmp=0;
                    dot = NODOT;
                    break;
case "1/x": //Reciprocal operation
                    if(opt!=NULL)
                    {
                        calc();
                    }
                    if (tmp != 0)
                    {
                        res = 1 / tmp;
                        //res=tmp;
                    }
                    else                   
                    res=1/res;
                    = ();
                    tmp=0;
                    dot = NODOT;
                    break;
                case ".":
// Clicked on the decimal point
                    if(dot==HASDOT)
                        return;
                    else
                    {
                        dot = HASDOT;
                        dotnum = 0;
                    }                   
                    break;
                case "+/-":
// Click on the symbol operation
                    if(tmp!=0)
                    {
                        tmp = -tmp;
                        = ();
                    }
                    else
                    {
                        res=-res;
                        //res=tmp;
                        = ();   
                    }
                    dot = NODOT;
                    break;   
case "CE":     //Clear operation
res = 0;                                                                                                                            �
tmp = 0;
opt = NULL;
dot = NODOT;    // Record whether the decimal point is clicked
num = 0;                         num = 0;                                                                                                 �
dotnum = 0;                                                                                                                           �
                    ="";                   
                    break;
case "C":
res = 0;                                                                                                                            �
tmp = 0;
opt = NULL;
dot = NODOT;    // Record whether the decimal point is clicked
num = 0;                         num = 0;                                                                                                 �
dotnum = 0;                                                                                                                           �
                    ="";
                    break;
            }
        }
        #endregion
#region     Main Menu Event
//Open the topic---Help of calling calculator in Windows XP
        private void menuItem5_Click(object sender, e)
        {
            (this,"C:\\WINDOWS\\Help\\");       
        }
//Opened on us
        private void menuItem6_Click(object sender, e)
        {
            Form2 fm=new Form2();
            ();
        }
//copy
        private void menuItem2_Click(object sender, e)
        {
            if(>0)
            {
                ();
            }
        }
        //
//Paste
        private void menuItem3_Click(object sender, e)
        {
            ();
        }
//Calling the calculator in Windows XP
        private void menuItem9_Click(object sender, e)
        {
             (this,"C:\\WINDOWS\\system32\\");
        }
//quit
        private void menuItem10_Click(object sender, e)
        {
            ();
        }
        #endregion
    }
}