Create A Calculator Using C# Application

In this blog, I am going to show you a simple Calculator, using C# Application. Here, you are able to Add, Subtract, Divide and Multiply the numbers, using C# Windows Application Code.
 
This is not a perfect calculator but it is able to perform most of the operations like Addition, Subtraction, Multplication and Division of the numbers.
 
Please follow the steps given below to create a calculator, using C# Application.
 
Step 1

Open your Visual Studio, create a New Project and name it  Calcualtor.
 
 
 
Step 2

Click OK button, which redirects you to Next Page, where you are able to create a page.
 
 

Step 3

Click Form, go to the Properties and change the Text properties name to Calculator.


Step 4

From the ToolBox, drag and drop a Textbox. Go to Properties of TextBox.
 


Step 5

In the image given below, we are able to change the Properties of TextBox. If you want to resize your Textbox, then change the Multiline Properties to True.

If you want to change the color of the Textbox, then go the Background color properties and change it. If you want to change the Alignment, go to the TextAlign properties and change it to Center.



Step7

Below the Textbox, you can drag and drop multiple buttons and resize it. Once you resize all the buttons, then you can change the text of the button .
 
Example: 1,2 3,4,5,6,7,8....
 
C : Reset button

 
Step 8

Finally, the page looks, as shown below.


Step9

You can add the code inside the button. 
  1. private void button8_Click(object sender, EventArgs e)  
  2.        {  
  3.            textBox1.Text = textBox1.Text + 2;  
  4.        }  
  5.   
  6.        private void button9_Click(object sender, EventArgs e)  
  7.        {  
  8.            textBox1.Text = textBox1.Text + 1;  
  9.        }  
  10.   
  11.        private void button13_Click(object sender, EventArgs e)  
  12.        {  
  13.            textBox1.Text = textBox1.Text + 3;  
  14.        }  
  15.   
  16.        private void button3_Click(object sender, EventArgs e)  
  17.        {  
  18.            textBox1.Text = textBox1.Text + 6;  
  19.        }  
  20.   
  21.        private void button2_Click(object sender, EventArgs e)  
  22.        {  
  23.            textBox1.Text = textBox1.Text + 5;  
  24.        }  
  25.   
  26.        private void button10_Click(object sender, EventArgs e)  
  27.        {  
  28.            textBox1.Text = textBox1.Text + 4;  
  29.        }  
  30.   
  31.        private void button6_Click(object sender, EventArgs e)  
  32.        {  
  33.            textBox1.Text = textBox1.Text + 9;  
  34.        }  
  35.   
  36.        private void button7_Click(object sender, EventArgs e)  
  37.        {  
  38.            textBox1.Text = textBox1.Text + 8;  
  39.        }  
  40.   
  41.        private void button1_Click(object sender, EventArgs e)  
  42.        {  
  43.            textBox1.Text = textBox1.Text + 7;  
  44.        }  
  45.   
  46.        private void button5_Click(object sender, EventArgs e)  
  47.        {  
  48.            textBox1.Text = textBox1.Text + 0;  
  49.        }  
  50.   
  51.        private void button16_Click(object sender, EventArgs e)  
  52.        {  
  53.            textBox1.Text = textBox1.Text + 0 + 0;  
  54.        }  
  1. private void btnPlus_Click(object sender, EventArgs e)  
  2.         {  
  3.             num1 = float.Parse(textBox1.Text);  
  4.             textBox1.Clear();  
  5.             textBox1.Focus();  
  6.             count = 2;  
  7.              
  8.         }  
  1. private void btnminus_Click(object sender, EventArgs e)  
  2.       {  
  3.           if (textBox1.Text != "")  
  4.           {  
  5.               num1 = float.Parse(textBox1.Text);  
  6.               textBox1.Clear();  
  7.               textBox1.Focus();  
  8.               count = 1;  
  9.           }  
  10.       }  
  1. private void btnmultiply_Click(object sender, EventArgs e)  
  2.         {  
  3.             num1 = float.Parse(textBox1.Text);  
  4.             textBox1.Clear();  
  5.             textBox1.Focus();  
  6.             count = 3;  
  7.         }  
  1. private void btndivide_Click(object sender, EventArgs e)  
  2.        {  
  3.            num1 = float.Parse(textBox1.Text);  
  4.            textBox1.Clear();  
  5.            textBox1.Focus();  
  6.            count = 4;  
  7.        }  
  1. private void btnequal_Click(object sender, EventArgs e)  
  2.         {  
  3.             compute(count);  
  4.         }  
  5.         public void compute(int count)  
  6.         {  
  7.             switch (count)  
  8.             {  
  9.                 case 1:  
  10.                     ans = num1 - float.Parse(textBox1.Text);  
  11.                     textBox1.Text = ans.ToString();  
  12.                     break;  
  13.                 case 2:  
  14.                     ans = num1 + float.Parse(textBox1.Text);  
  15.                     textBox1.Text = ans.ToString();  
  16.                     break;  
  17.                 case 3:  
  18.                     ans = num1 * float.Parse(textBox1.Text);  
  19.                     textBox1.Text = ans.ToString();  
  20.                     break;  
  21.                 case 4:  
  22.                     ans = num1 / float.Parse(textBox1.Text);  
  23.                     textBox1.Text = ans.ToString();  
  24.                     break;  
  25.                 default:  
  26.                     break;  
  27.             }  
  28.         }  
  1. private void btnC_Click(object sender, EventArgs e)  
  2.        {  
  3.            textBox1.Clear();  
  4.            count = 0;   
  5.        }  
  1. private void button15_Click(object sender, EventArgs e)  
  2.        {  
  3.            int c = textBox1.TextLength;  
  4.            int flag = 0;  
  5.            string text = textBox1.Text;  
  6.            for (int i = 0; i < c; i++)  
  7.            {  
  8.                if (text[i].ToString() == ".")  
  9.                {  
  10.                    flag = 1; break;  
  11.                }  
  12.                else  
  13.                {  
  14.                    flag = 0;  
  15.                }  
  16.            }  
  17.            if (flag == 0)  
  18.            {  
  19.                textBox1.Text = textBox1.Text + ".";  
  20.            }  
  21.        }  
Before starting with the code, you should declare the variable globally.
  1. public Form1()  
  2.        {  
  3.   
  4.            InitializeComponent();  
  5.   
  6.        }  
  7.        float num1, ans;  
  8.        int count;  
The final output will look, as shown below.