Creating a Small Calculator in C# Visual Studio

Creating a Small Calculator in C#

Let’s create a small calculator in this tutorial to get hands-on experience in Visual C# with some basic coding. Click here if you want to get started with visual studio

Create a new project and click OK giving the name SmallCalculator

Once you click OK, setup your development environment as below

Now, in properties pane, change the Text Property to Small Calculator

Now, from the Toolbox add 2 Labels (Select Label Control from Toolbox and Click on the Form) and change their Text Properties to First Number and Second Number respectively

Then, Add 2 Textbox controls from Toolbox like you added 2 labels and the form should look like below at this stage

Now add 1 more TextBox control from Toolbox and make its ReadOnly Property to True

Now Add 5 Button controls from Toolbox and change their Text Properties + , – , X , / , CLEAR as shown in below figure

Here we have added 5 Button Controls and purposes of them in our application

+ –> When the + button is clicked First Number & Second Number should be added and the total should be displayed in TextBox 3.

– –>  When the – button is clicked Second Number should be deducted from First Number and the answer should be displayed in TextBox 3.

X –> When the X button is clicked First Number & Second Number should be multiplied and the answer should be displayed in TextBox 3.

/ –> When the + button is clicked First Number should be divided by Second Number and the answer should be displayed in TextBox 3.

CLEAR –> When the CLEAR button is clicked all the Texts from the textboxes should be cleared.

Events in Programming

While you might visualize a Visual Studio project as a series of procedures that execute in a sequence, in reality, most programs are event driven—meaning the flow of execution is determined by external occurrences called events.

An event is a signal that informs an application that something important has occurred. For example, when a user clicks a control on a form, the form can raise a Click event and call a procedure that handles the event. Events also allow separate tasks to communicate. Say, for example, that your application performs a sort task separately from the main application. If a user cancels the sort, your application can send a cancel event instructing the sort process to stop.

Now, in the above application we are going to use OnClick Events. Meaning, when we click the button the event is going to be triggered (The code within the button is going to be executed)

Now let’s write some basic codes to serve our purpose of the application.

Double Click on the + button to get the code view to write the code for the + Button

Write the following code inside the + Button (where the cursor is blinking)

int fnumber, snumber, total;
fnumber = Convert.ToInt32(textBox1.Text);
snumber = Convert.ToInt32(textBox2.Text);
total = fnumber + snumber;
textBox3.Text = total.ToString();

Once you type the above code inside the + Button, Run your application by pressing F6 Key or Start button. Now, type some numeric data in First Number TextBox and Second Number TextBox and click the + Button. It will do the math and display the total.

Congratulations. If it sums up as it should do, you have done it correctly and you have learnt lot of things.

Now let’s go to the code segment to know what these lines of codes are doing

int fnumber, snumber, total;

In this line of code we are creating three variables namely fnumber, snumber & total with the data type int. Check How do you do it in Java

fnumber = Convert.ToInt32(textBox1.Text);

textBox1 is the name of the textbox (First Number). To get the data typed in the textbox we use the method .Text. So in order to get the text types in textBox1 we use textBox1.Text. However, whatever the data we type in textbox controls by default, they are String data types. Meaning, with that data we can’t do calculations. Hence, we need to do type conversion from String to Text.


We are converting the textBox1.Text data to int data type.

fnumber = Convert.ToInt32(textBox1.Text);

Now we are assigning that value to the variable fnumber.

snumber = Convert.ToInt32(textBox2.Text);

We are repeating the same process of fnumber but for the textBox2.Text

total = fnumber + snumber;

In the above line, all three variables are of int data type as we defined them ourselves above.

Now the variable will hold the value by addming fnumber and snumber. However, it is of int data type.

Now if we want to display the value of total, we can’t directly display it because the textBox controls will only accept String type of data. Thus, we are again converting total (int type data) to String type data by .ToString() Method with the below line

textBox3.Text = total.ToString();

With this knowledge, try to write the code for -, x, / buttons.

Write the following code inside the CLEAR button to clear all text fields


Now your small calculator in C# ready to calculate. Try to extend the calculator by adding additional features. Good Luck and Get Connected with to be inspired by technologies.


Please enter your comment!
Please enter your name here