First I want to say thank you for taking the time to read this tutorial. If your a beginner this will help you a lot in the basics of visual basic and also create a fun little application. In this tutorial we will be creating a calculator in visual basic. For this calculator we will use radio buttons, text boxes and buttons to create a fully functional calculator. Hopefully this will intrigue you into making other fun little applications, for me, the better you make your projects the better I feel mentally and physically.

1. Open Microsoft Visual Studio.

2. Click File > New > Project.

3. In installed templates, click on Other Languages > Visual Basic > Windows. Click on Windows Forms Application, name the file Calculator in Visual Basic and click OK.

You will see a blank form like this:

form1

We will add radio buttons, text boxes and buttons to the form to create the design of the calculator. Keep in mind you can design your calculator the way you want, it does not have to be identical to mine, the functionality will be the same regardless of the calculator’s design.

4. In the Toolbox panel, click and drag a RadioButton onto the form. Copy the radio button and paste it onto the form three more times, until there is a total of four radio buttons on the form.

5. In the properties window of RadioButton1, change it’s text to Add and change it’s name to addRb.
In the properties window of RadioButton2, change it’s text to Subtract and change it’s name to subtractRb.
In the properties window of RadioButton3, change it’s text to Multiply and change it’s name to multiplyRb.
In the properties window of RadioButton4, change it’s text to Divide and change it’s name to divideRb.

6. In the Toolbox panel, click and drag a TextBox onto the form. Copy the text box and paste it onto the form. You should have a total of two text boxes on the form. In the properties window of TextBox1, rename it userInput1TextBox. In the properties window to TextBox2, rename it userInput2TextBox.

7. In the Toolbox panel, click and drag a Button control onto the form. Copy the button and paste it onto the form. You should have a total of two buttons on the form. In the properties window of the Button1, change it’s text to Calculate and it’s name to calculateBtn. In the properties window of Button2, change it’s text to Clear and it’s name to clearBtn.

8.In the Toolbox panel, click and drag a Label onto the form. Copy the label and paste it onto the form. You should have a total of two labels on the form. In the properties window of Label1, change it’s name to symbolLbl and delete it’s text so it is blank. In the properties window of Label2, change it’s name to answerLbl and delete it’s text so it is blank. We will use symbolLbl for the operation symbol, so place it between the text boxes where it makes most sense. The answerLbl will be used to output the answer, so place it where it makes most sense.

This is what my design looks like, there is one label in between the two text boxes and one to the right of the calculate button. Remember that you can design it any way you’d like.

form buttons

This is where we actually do the coding that does the calculating. We will make great use of the If Else statement to make sure which radio button is checked and to put code in accordingly.

9. Double click on the Calculate button to take you to Form1.vb. You will see a calculateBtn_Click method, inside of this method is where we will enter the code. Although this is a basic calculator, I took the liberty to have a message box appear when a number is not entered or no operation is selected. Also we all know that when a zero is involved when multiplying or dividing, the answer will always be zero. In the case that a denominator is zero I’ve programmed the application to read “Cannot Divide by Zero” because you cannot. The rest of the code is just calculating the user inputs depending on which radio button is selected.

Now that we have the calculating part of the coding done, we will work on the clear button. The function of this button will be to clear what is inside the text boxes, the labels and the operation selection.

10. So let’s start by going to Form1.vb [Design] and double click on the Clear button. We will basically set the text values of the text boxes and labels to null while also checking for which radio button is selected and changing it to false.

Now that we have the clear button finished, we still have to code the symbolLbl to change symbols according to which operation is selected.

11. Switch to Form1.vb [Design] and double click on the Add radio button. We want to tell the application to change symbolLbl to the correct operation symbol each time a radio button is selected. So to do that we will set the test of symbolLbl to “+” and the text of answerLbl to “”(nothing).

We will do the same for the other radio buttons, just change the symbolLbl according to which operation is selected.

So when all the coding is done you should have the same code that is in the code block:

Run the application. Test the application and try to break it. It should be fully functional. Try entering only one number and don’t select an operation to see if the message boxes appear each time. Use the clear button to clear everything from the calculator and use the calculate button to calculate the and output the correct answer. Everything works. This was a fun experiment, you learned a little bit about If Else statements but also about radio buttons, text boxes and buttons and you have a calculator to use whenever your in need of one!

Download Source Files