Create Basic Calculator Using Windows Forms And C#

This app is capable of performing addition, subtraction, multiplication as well as division. So let’s get started.

Step 1

Open your Visual Studio or Visual C# Express Edition and create a new project.



Set its type to Windows Forms Application and set its name to CalculatorApp. Press OK.

You should see the main form on the workspace as follows:



Step 2

Change the form text property to Calculator, because we don’t want our application to have the title Form1 when it starts.



Step 3

From the ToolBox, drop a TextBox control onto the form, and resize it. Perform some changes in properties as shown in figure.



Step 4

Now, we start working with the display, usually when the calculator starts it should display the number 0. In our case it does not. So we modify the Text Property and write 0 in that. Make sure you don’t add any spaces before or after the 0.

The second thing to note is that the number is aligned to the left, while calculators align the number to the right. Search for the TextAlign property and change it to Right.



This is what the window will look like.

Step 5

From the tool box window, drag and drop a Button onto the form. Change its Name property to n1. This will help us identify which number was pressed. Change the backcolor property for the button. After the color changes, we will modify the text that the button is displaying, so change the text property into 1. Go to the font property, and set its font to courier new, and size to 16 for example.



Now we can repeat the same operation to all the other nine buttons, or we can just copy this button and get the same result quickly. Just hold the ctrl key and drag the control.

Change the names of the buttons (or you can leave them as they are and skip this part). The names will continue to be n2, n3, n4, n5, n6, n7, n8, n9, and n0.The window should look like this.



Step 6

Double click the button n1 to go to its event. The n1_Click is the name of this procedure. It tells you that it get executed when the button whose name n1 is clicked by the user. Write down the code to have it like this:
  1. private void n1_Click(object sender, EventArgs e)  
  2. {  
  3.     if (textBox1.Text == "0" && textBox1.Text != null)  
  4.     {  
  5.         textBox1.Text = "1";  
  6.     }  
  7.     else  
  8.     {  
  9.         textBox1.Text = textBox1.Text + "1";  
  10.     }  
  11. }  
You should repeat the code for buttons n2,n3,n4,n5,n6,n7,n8,n9, and n0 to add the numbers 2,3,4,5,6,7,8,9,0 respectively. So the code should look like this:
  1. private void n1_Click(object sender, EventArgs e)  
  2. {  
  3.     if (textBox1.Text == "0" && textBox1.Text != null)  
  4.     {  
  5.         textBox1.Text = "1";  
  6.     }  
  7.     else  
  8.     {  
  9.         textBox1.Text = textBox1.Text + "1";  
  10.     }  
  11. }  
  12. private void n2_Click(object sender, EventArgs e)  
  13. {  
  14.     if (textBox1.Text == "0" && textBox1.Text != null)  
  15.     {  
  16.         textBox1.Text = "2";  
  17.     }  
  18.     else  
  19.     {  
  20.         textBox1.Text = textBox1.Text + "2";  
  21.     }  
  22. }  
  23.   
  24. private void n3_Click(object sender, EventArgs e)  
  25. {  
  26.     if (textBox1.Text == "0" && textBox1.Text != null)  
  27.     {  
  28.         textBox1.Text = "3";  
  29.     }  
  30.     else  
  31.     {  
  32.         textBox1.Text = textBox1.Text + "3";  
  33.     }  
  34. }  
  35.   
  36. private void n4_Click(object sender, EventArgs e)  
  37. {  
  38.     if (textBox1.Text == "0" && textBox1.Text != null)  
  39.     {  
  40.         textBox1.Text = "4";  
  41.     }  
  42.     else  
  43.     {  
  44.         textBox1.Text = textBox1.Text + "4";  
  45.     }  
  46. }  
  47.   
  48. private void n5_Click(object sender, EventArgs e)  
  49. {  
  50.     if (textBox1.Text == "0" && textBox1.Text != null)  
  51.     {  
  52.         textBox1.Text = "5";  
  53.     }  
  54.     else  
  55.     {  
  56.         textBox1.Text = textBox1.Text + "5";  
  57.     }  
  58. }  
  59.   
  60. private void n6_Click(object sender, EventArgs e)  
  61. {  
  62.     if (textBox1.Text == "0" && textBox1.Text != null)  
  63.     {  
  64.         textBox1.Text = "6";  
  65.     }  
  66.     else  
  67.     {  
  68.         textBox1.Text = textBox1.Text + "6";  
  69.     }  
  70. }  
  71.   
  72. private void n7_Click(object sender, EventArgs e)  
  73. {  
  74.     if (textBox1.Text == "0" && textBox1.Text != null)  
  75.     {  
  76.         textBox1.Text = "7";  
  77.     }  
  78.     else  
  79.     {  
  80.         textBox1.Text = textBox1.Text + "7";  
  81.     }  
  82. }  
  83. private void n8_Click(object sender, EventArgs e)  
  84. {  
  85.     if (textBox1.Text == "0" && textBox1.Text != null)  
  86.     {  
  87.         textBox1.Text = "8";  
  88.     }  
  89.     else  
  90.     {  
  91.         textBox1.Text = textBox1.Text + "8";  
  92.     }  
  93. }  
  94. private void n9_Click(object sender, EventArgs e)  
  95. {  
  96.     if (textBox1.Text == "" && textBox1.Text != null)  
  97.     {  
  98.         textBox1.Text = "9";  
  99.     }  
  100.     else  
  101.     {  
  102.         textBox1.Text = textBox1.Text + "9";  
  103.     }  
  104. }  
  105.   
  106. private void n0_Click(object sender, EventArgs e)  
  107. {  
  108.     textBox1.Text = textBox1.Text + "0";  
  109. }  
Now run the application and click few buttons:



Step 7

Next we create the clear button, and place the copy next to the 0. Change its name to bc, and text property to C. Similarly create add, sub, mul and div button as shown below:



Change the text property.name them as follows. 
  • + name is bad
  • - name is bsub
  • * name is bmult
  • / name is bdiv
  • = name is bequal
Step 8

Double click on each of the event handler and the code. So our final code should look like this":
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8.   
  9. namespace CalculatorApp  
  10. {  
  11.     public partial class Form1 : Form  
  12.     {  
  13.         double FirstNumber;  
  14.         string Operation;  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.   
  20.          
  21.         private void n1_Click(object sender, EventArgs e)  
  22.         {  
  23.             if (textBox1.Text == "0" && textBox1.Text != null)  
  24.             {  
  25.                 textBox1.Text = "1";  
  26.             }  
  27.             else  
  28.             {  
  29.                 textBox1.Text = textBox1.Text + "1";  
  30.             }  
  31.         }  
  32.   
  33.         private void n2_Click(object sender, EventArgs e)  
  34.         {  
  35.             if (textBox1.Text == "0" && textBox1.Text != null)  
  36.             {  
  37.                 textBox1.Text = "2";  
  38.             }  
  39.             else  
  40.             {  
  41.                 textBox1.Text = textBox1.Text + "2";  
  42.             }  
  43.         }  
  44.   
  45.         private void n3_Click(object sender, EventArgs e)  
  46.         {  
  47.             if (textBox1.Text == "0" && textBox1.Text != null)  
  48.             {  
  49.                 textBox1.Text = "3";  
  50.             }  
  51.             else  
  52.             {  
  53.                 textBox1.Text = textBox1.Text + "3";  
  54.             }  
  55.         }  
  56.   
  57.         private void n4_Click(object sender, EventArgs e)  
  58.         {  
  59.             if (textBox1.Text == "0" && textBox1.Text != null)  
  60.             {  
  61.                 textBox1.Text = "4";  
  62.             }  
  63.             else  
  64.             {  
  65.                 textBox1.Text = textBox1.Text + "4";  
  66.             }  
  67.         }  
  68.   
  69.         private void n5_Click(object sender, EventArgs e)  
  70.         {  
  71.             if (textBox1.Text == "0" && textBox1.Text != null)  
  72.             {  
  73.                 textBox1.Text = "5";  
  74.             }  
  75.             else  
  76.             {  
  77.                 textBox1.Text = textBox1.Text + "5";  
  78.             }  
  79.         }  
  80.   
  81.         private void n6_Click(object sender, EventArgs e)  
  82.         {  
  83.             if (textBox1.Text == "0" && textBox1.Text != null)  
  84.             {  
  85.                 textBox1.Text = "6";  
  86.             }  
  87.             else  
  88.             {  
  89.                 textBox1.Text = textBox1.Text + "6";  
  90.             }  
  91.         }  
  92.   
  93.         private void n7_Click(object sender, EventArgs e)  
  94.         {  
  95.             if (textBox1.Text == "0" && textBox1.Text != null)  
  96.             {  
  97.                 textBox1.Text = "7";  
  98.             }  
  99.             else  
  100.             {  
  101.                 textBox1.Text = textBox1.Text + "7";  
  102.             }  
  103.         }  
  104.   
  105.         private void n8_Click(object sender, EventArgs e)  
  106.         {  
  107.             if (textBox1.Text == "0" && textBox1.Text != null)  
  108.             {  
  109.                 textBox1.Text = "8";  
  110.             }  
  111.             else  
  112.             {  
  113.                 textBox1.Text = textBox1.Text + "8";  
  114.             }  
  115.         }  
  116.   
  117.         private void n9_Click(object sender, EventArgs e)  
  118.         {  
  119.             if (textBox1.Text == "" && textBox1.Text != null)  
  120.             {  
  121.                 textBox1.Text = "9";  
  122.             }  
  123.             else  
  124.             {  
  125.                 textBox1.Text = textBox1.Text + "9";  
  126.             }  
  127.         }  
  128.   
  129.         private void n0_Click(object sender, EventArgs e)  
  130.         {  
  131.             textBox1.Text = textBox1.Text + "0";  
  132.         }  
  133.   
  134.         private void bad_Click(object sender, EventArgs e)  
  135.         {  
  136.             FirstNumber = Convert.ToDouble(textBox1.Text);  
  137.             textBox1.Text = "0";  
  138.             Operation = "+";  
  139.         }  
  140.   
  141.         private void bsub_Click(object sender, EventArgs e)  
  142.         {  
  143.             FirstNumber = Convert.ToDouble(textBox1.Text);  
  144.             textBox1.Text = "0";  
  145.             Operation = "-";  
  146.   
  147.         }  
  148.   
  149.         private void bmul_Click(object sender, EventArgs e)  
  150.         {  
  151.             FirstNumber = Convert.ToDouble(textBox1.Text);  
  152.             textBox1.Text = "0";  
  153.             Operation = "*";  
  154.         }  
  155.   
  156.         private void bdiv_Click(object sender, EventArgs e)  
  157.         {  
  158.             FirstNumber = Convert.ToDouble(textBox1.Text);  
  159.             textBox1.Text = "0";  
  160.             Operation = "/";  
  161.         }  
  162.   
  163.         private void bc_Click(object sender, EventArgs e)  
  164.         {  
  165.             textBox1.Text = "0";  
  166.         }  
  167.   
  168.         private void ndot_Click(object sender, EventArgs e)  
  169.         {  
  170.             textBox1.Text = textBox1.Text + ".";  
  171.         }  
  172.   
  173.         private void nequal_Click(object sender, EventArgs e)  
  174.         {  
  175.             double SecondNumber;  
  176.             double Result;  
  177.   
  178.             SecondNumber = Convert.ToDouble(textBox1.Text);  
  179.   
  180.             if (Operation == "+")  
  181.             {  
  182.                 Result = (FirstNumber + SecondNumber);  
  183.                 textBox1.Text = Convert.ToString(Result);  
  184.                 FirstNumber = Result;  
  185.             }  
  186.             if (Operation == "-")  
  187.             {  
  188.                 Result = (FirstNumber - SecondNumber);  
  189.                 textBox1.Text = Convert.ToString(Result);  
  190.                 FirstNumber = Result;  
  191.             }  
  192.             if (Operation == "*")  
  193.             {  
  194.                 Result = (FirstNumber * SecondNumber);  
  195.                 textBox1.Text = Convert.ToString(Result);  
  196.                 FirstNumber = Result;  
  197.             }  
  198.             if (Operation == "/")  
  199.             {  
  200.                 if (SecondNumber == 0)  
  201.                 {  
  202.                     textBox1.Text = "Cannot divide by zero";  
  203.   
  204.                 }  
  205.                 else  
  206.                 {  
  207.                     Result = (FirstNumber / SecondNumber);  
  208.                     textBox1.Text = Convert.ToString(Result);  
  209.                     FirstNumber = Result;  
  210.                 }  
  211.             }  
  212.         }  
  213.   
  214.                
  215.     }  
  216. }  
Step 9

Finally we will modify some form behavior so that it does not change size.
  1. Change the FormBorderStyle property from Sizable to FixedSingle, this will prevent your calculator from being resized.
  2. Finally change the MaximizeBox for your form to be False. This prevents the form from being maximized.
  3. Change Backcolor property as you like.
  4. Add a status strip label by dragging and dropping it from the toolbox and change it’s text property as you like.

So this is how our calculator should look.

Up Next
    Ebook Download
    View all
    Learn
    View all