C# Tutorial, Lesson 18: Building Windows Forms Calculator with C#.




The calculator consists of a form, a display window and buttons.  To create a button  we need to have a Form to which we will attach it. We subscribe the  button to the message queue to handle button clicks with mouse. The calculator needs a window to display results of the calculations, and we use a TextBox as such a window. The program is pretty simple but it takes quite a few lines. There are two classes in the program. Class test inherits from Form, and this is where most of the work is  done. Let's start with a simpler class myButton. Constructor of this class takes coordinates of the button as a pair of integers. It also takes the name of the button as an integer or a string. Four things happen inside the constructor of myButton: 


Class test should inherit from Form to specify the Form's properties and methods. 

using System;
using System.Windows.Forms;

class myButton{
public Button name;
//constructor of myButton takes coordinates of the button and its integer name as arguments
    public myButton(int x, int y,int k)
    {
        this.name=new System.Windows.Forms.Button(); //create an instance of Button
        name.Size=new System.Drawing.Size(25,25);//specify the button size
        name.Text=k.ToString();//name of the button
        name.Location=new System.Drawing.Point(x,y);//location of the button
    } //constructor of myButton takes coordinates of the button and its string name as arguments
    public myButton(int x,int y,string k)
    {
        this.name=new System.Windows.Forms.Button(); //create an instance of Button
        name.Size=new System.Drawing.Size(25,25);//specify the button size
        name.Text=k; //name of the button
        name.Location=new System.Drawing.Point(x,y);//location of the button
    }
}

class test:Form
{
    private System.Windows.Forms.TextBox text; //declare textbox
    private long x=0,temp;
    private char op;
    public test()
    {//create buttons in different places of the form
        myButton mbutton7=new myButton(25,25,7);
        myButton mbutton8=new myButton(50,25,8);
        myButton mbutton9=new myButton(75,25,9);
        myButton mbutton4=new myButton(25,50,4);
        myButton mbutton5=new myButton(50,50,5);
        myButton mbutton6=new myButton(75,50,6);
        myButton mbutton1=new myButton(25,75,1);
        myButton mbutton2=new myButton(50,75,2);
        myButton mbutton3=new myButton(75,75,3);
        myButton mbutton0=new myButton(100,75,0);
        myButton mbutton_plus=new myButton(25,100,"+");
        myButton mbutton_equal=new myButton(50,100,"=");
        myButton mbutton_clear=new myButton(75,100,"C");
        myButton mbutton_minus=new myButton(100,100,"-");
        myButton mbutton_multiply=new myButton(100,50,"*");
        myButton mbutton_divide=new myButton(100,25,"/");
        this.Text="Calculator."; //specify the name of the form

        this.MaximizeBox=false; //do not allow maximizing of the calculator
        this.BorderStyle=FormBorderStyle.FixedDialog; //do not allow resizing
        this.Size=new System.Drawing.Size(150,200); //vertical and
//horizontal size in pixels
//attach buttons to the calculator form one by one.

        this.Controls.Add(mbutton0.name);
        this.Controls.Add(mbutton1.name);
        this.Controls.Add(mbutton2.name);
        this.Controls.Add(mbutton3.name);
        this.Controls.Add(mbutton4.name);
        this.Controls.Add(mbutton5.name);
        this.Controls.Add(mbutton6.name);
        this.Controls.Add(mbutton7.name);
        this.Controls.Add(mbutton8.name);
        this.Controls.Add(mbutton9.name);
        this.Controls.Add(mbutton_plus.name);
        this.Controls.Add(mbutton_minus.name);
        this.Controls.Add(mbutton_equal.name);
        this.Controls.Add(mbutton_clear.name);
        this.Controls.Add(mbutton_minus.name);
        this.Controls.Add(mbutton_multiply.name);
        this.Controls.Add(mbutton_divide.name);

        this.text=new System.WinForms.TextBox(); //create TextBox text
        text.Location=new System.Drawing.Point(25,0); //specify location of the text
        this.Controls.Add(text); //Attach TextBox to the Form
        //handle button clicks
        mbutton0.name.Click+=new EventHandler(this.button_method);
        mbutton1.name.Click+=new EventHandler(this.button_method);
        mbutton2.name.Click+=new EventHandler(this.button_method);
        mbutton3.name.Click+=new EventHandler(this.button_method);
        mbutton4.name.Click+=new EventHandler(this.button_method);
        mbutton5.name.Click+=new EventHandler(this.button_method);
        mbutton6.name.Click+=new EventHandler(this.button_method);
        mbutton7.name.Click+=new EventHandler(this.button_method);
        mbutton8.name.Click+=new EventHandler(this.button_method);
        mbutton9.name.Click+=new EventHandler(this.button_method);
        mbutton_plus.name.Click+=newSystem.EventHandler(this.button_methodoper);
        mbutton_equal.name.Click+=new System.EventHandler(this.button_methodoper);
        mbutton_clear.name.Click+=new System.EventHandler(this.button_methodoper);
        mbutton_minus.name.Click+=new     System.EventHandler(this.button_methodoper);
        mbutton_multiply.name.Click+=new System.EventHandler(this.button_methodoper);
        mbutton_divide.name.Click+=new System.EventHandler(this.button_methodoper);
    }
    public void button_method(object sender, EventArgs e)
    {
        //find out which button was clicked
        string tem=sender.ToString();
        int k=tem[tem.Length-1]-48; //index is zero based convert integer
        //assuming ASCII incoding
        temp=k+10*temp;
        text.Text=temp.ToString();
    }

    long oper(long x,long y,char sig) //perform an operation depending on the button
    {
    if(sig=='+')
        return x+y;
    else if(sig=='-')
        return x-y;
    else if(sig=='*')
        return x*y;
    else if(sig=='/')
        return x/y;
    else
        return 0;
    }

    public void button_methodoper(object sender, EventArgs e)
    {
        string tem=sender.ToString();
        char k=tem[tem.Length-1];//index is zero based convert integer
        //assuming ASCII incoding
        if(k=='=')
        {
            if(x==0)
            {
                    x=temp;
                    temp=0;
            }
            else
            {
                x=oper(x,temp,op);
                temp=0;
                text.Text=x.ToString();
               }
            }
            else if (k=='C') //clear all stored variables
            {
                x=0;
                temp=0;
                text.Text=x.ToString();
            }
            else
            {
                if(x==0)
                 {
                    x=temp;
                    temp=0;
                    op=k;
                   }
                else
                {
                    if(temp!=0){
                    x=oper(x,temp,op);
                    temp=0;
                    text.Text=x.ToString();
                    }
                op=k;//store operation
                }
            }
    }

    public void form_method(object sender, EventArgs e) //method to be passed to form
//handler delegate it is activated every time a mouse is clicked on the form's surface.
    {}
    public static void Main()
    {
        test t=new test();
        t.ShowDialog();
    }
}


Copyright© 2001-2006 Aleksey Nudelman