Introduction

Simple calculator in windows form application c#, able to perform basic operations

Building the Sample

-> just download file

-> unzip it

-> open .sln file

-> build solution 

-> run

Description

this is just very simple calculator easy to made

 

Buttons 1,2,3,4,5,6,7,8,9,0,dot(.) are assigned same event handler.

 

  private void button_click(object sender, EventArgs e)
        {
            if ((inputBox.Text == "0") || (oper_pressed))
            {
                inputBox.Clear();
                dotCount = 0;
            }
               
            oper_pressed = false;
            Button b = (Button)sender;
            if(b.Text=="." && dotCount<1)
            {
                dotCount++;
                inputBox.Text = inputBox.Text + b.Text;
            }
            if(b.Text!=".")
            {
                inputBox.Text = inputBox.Text + b.Text;
            }
          
        }

 

Buttons (+,-,*,/) are assigned to another same event handler

 

 private void operator_click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            operation = b.Text;
            val = Double.Parse(inputBox.Text);
            oper_pressed = true;
            equation.Text = val + " " + operation;
        }


when equal button is pressed

 

 

C#
Edit|Remove
privatevoid equal_click(object sender, EventArgs e) 
        { 
            equation.Text = ""; 
            switch (operation) 
            { 
                case"+": 
                    inputBox.Text = (val + Double.Parse(inputBox.Text)).ToString(); 
                    break; 
                case"-": 
                    inputBox.Text = (val - Double.Parse(inputBox.Text)).ToString(); 
                    break; 
                case"*": 
                    inputBox.Text = (val * Double.Parse(inputBox.Text)).ToString(); 
                    break; 
                case"/": 
                    inputBox.Text = (val / Double.Parse(inputBox.Text)).ToString(); 
                    break; 
                default: 
                    break; 
            } 
            double ans = double.Parse(inputBox.Text); 
            if(ans==(double)ans) 
            { 
                dotCount = 1; 
            } 
            else 
            dotCount = 0; 
               
        }

 

 

To restrict user that he/she cannot put multiple times dot (.) as input. we add a following check that counts how many times he entered dot(.)

 

 

C#
Edit|Remove
    if(b.Text=="." && dotCount<1) 
            { 
                dotCount++; 
                inputBox.Text = inputBox.Text + b.Text; 
            } 
            if(b.Text!=".") 
            { 
                inputBox.Text = inputBox.Text + b.Text; 
            }
 

 

C#
Edit|Remove
   double ans = double.Parse(inputBox.Text); 
            if(ans==(double)ans) 
            { 
                dotCount = 1; 
            } 
            else 
            dotCount = 0;

 

 

for full understanding download full source code and review it.

 

feel free to give feedback

email me:

umairnadeem20@hotmail.com