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:

Yes, it is possible to find a good web host. Sometimes it takes a while to find one you are comfortable with. After trying several, we went with Server Intellect and have been very happy thus far. They are by far the most professional, customer service friendly and technically knowledgeable host we’ve found so far.

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.

Need help with cloud hosting? Try Server Intellect. We used them for our cloud hosting services and we are very happy with the results!

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.

Code Block
Default.aspx
Public Class Form1

    Private Sub calculateBtn_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles calculateBtn.Click
        Dim number1 As Double
        Dim number2 As Double
        Dim answer As Double

        If input1TextBox.Text = "" OrElse input2TextBox.Text = "" Then
            MessageBox.Show("Please Enter a Number")
        ElseIf additionRb.Checked = False And subtractRb.Checked = False And multiplyRb.Checked = False And divideRb.Checked = False Then
            MessageBox.Show("Please Choose an Operation")
        Else
            number1 = Double.Parse(input1TextBox.Text)
            number2 = Double.Parse(input2TextBox.Text)

            If additionRb.Checked = True Then
                answer = number1 + number2
                answerLbl.Text = answer.ToString()
            ElseIf subtractRb.Checked = True Then
                answer = number1 - number2
                answerLbl.Text = answer.ToString()
            ElseIf multiplyRb.Checked = True Then
                If number1 = 0 OrElse number2 = 0 Then
                    answerLbl.Text = "0"
                Else
                    answer = number1 * number2
                    answerLbl.Text = answer.ToString()
                End If
            ElseIf divideRb.Checked = True Then
                If number1 = 0 Then
                    answerLbl.Text = "0"
                ElseIf number2 = 0 Then
                    answerLbl.Text = "Cannot Divide by Zero"
                Else
                    answer = number1 / number2
                    answerLbl.Text = answer.ToString()
                End If
            End If
        End If

    End Sub

End Class

I just signed up at Server Intellect and couldn’t be more pleased with my fully scalable & redundant cloud hosting! Check it out and see for yourself.


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.

Code Block
Default.aspx
    Private Sub clearBtn_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles clearBtn.Click
        input1TextBox.Text = ""
        input2TextBox.Text = ""
        answerLbl.Text = ""

        If additionRb.Checked = True Then
            additionRb.Checked = False
            symbolLbl.Text = ""
        ElseIf subtractRb.Checked = True Then
            subtractRb.Checked = False
            symbolLbl.Text = ""
        ElseIf multiplyRb.Checked = True Then
            multiplyRb.Checked = False
            symbolLbl.Text = ""
        Else
            divideRb.Checked = False
            symbolLbl.Text = ""
        End If
    End Sub

We used over 10 web hosting companies before we found Server Intellect. Our new server with cloud hosting,was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect’s customer support and assistance are the best we’ve ever experienced.

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.
Code Block
Default.aspx
    Private Sub additionRb_CheckedChanged(ByVal sender As System.ObjectByVal e As System.EventArgsHandles additionRb.CheckedChanged
        symbolLbl.Text = "+"
        answerLbl.Text = ""
    End Sub

    Private Sub subtractRb_CheckedChanged(ByVal sender As System.ObjectByVal e As System.EventArgsHandles subtractRb.CheckedChanged
        symbolLbl.Text = "-"
        answerLbl.Text = ""
    End Sub

    Private Sub multiplyRb_CheckedChanged(ByVal sender As System.ObjectByVal e As System.EventArgsHandles multiplyRb.CheckedChanged
        symbolLbl.Text = "*"
        answerLbl.Text = ""
    End Sub

    Private Sub divideRb_CheckedChanged(ByVal sender As System.ObjectByVal e As System.EventArgsHandles divideRb.CheckedChanged
        symbolLbl.Text = "/"
        answerLbl.Text = ""
    End Sub
So when all the coding is done you should have the same code that is in the code block:
Code Block
Default.aspx
Public Class Form1

    Private Sub calculateBtn_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles calculateBtn.Click
        Dim number1 As Double
        Dim number2 As Double
        Dim answer As Double

        If input1TextBox.Text = "" OrElse input2TextBox.Text = "" Then
            MessageBox.Show("Please Enter a Number")
        ElseIf additionRb.Checked = False And subtractRb.Checked = False And multiplyRb.Checked = False And divideRb.Checked = False Then
            MessageBox.Show("Please Choose an Operation")
        Else
            number1 = Double.Parse(input1TextBox.Text)
            number2 = Double.Parse(input2TextBox.Text)

            If additionRb.Checked = True Then
                answer = number1 + number2
                answerLbl.Text = answer.ToString()
            ElseIf subtractRb.Checked = True Then
                answer = number1 - number2
                answerLbl.Text = answer.ToString()
            ElseIf multiplyRb.Checked = True Then
                If number1 = 0 OrElse number2 = 0 Then
                    answerLbl.Text = "0"
                Else
                    answer = number1 * number2
                    answerLbl.Text = answer.ToString()
                End If
            ElseIf divideRb.Checked = True Then
                If number1 = 0 Then
                    answerLbl.Text = "0"
                ElseIf number2 = 0 Then
                    answerLbl.Text = "Cannot Divide by Zero"
                Else
                    answer = number1 / number2
                    answerLbl.Text = answer.ToString()
                End If
            End If
        End If

    End Sub

    Private Sub additionRb_CheckedChanged(ByVal sender As System.ObjectByVal e As System.EventArgsHandles additionRb.CheckedChanged
        symbolLbl.Text = "+"
        answerLbl.Text = ""
    End Sub

    Private Sub subtractRb_CheckedChanged(ByVal sender As System.ObjectByVal e As System.EventArgsHandles subtractRb.CheckedChanged
        symbolLbl.Text = "-"
        answerLbl.Text = ""
    End Sub

    Private Sub multiplyRb_CheckedChanged(ByVal sender As System.ObjectByVal e As System.EventArgsHandles multiplyRb.CheckedChanged
        symbolLbl.Text = "*"
        answerLbl.Text = ""
    End Sub

    Private Sub divideRb_CheckedChanged(ByVal sender As System.ObjectByVal e As System.EventArgsHandles divideRb.CheckedChanged
        symbolLbl.Text = "/"
        answerLbl.Text = ""
    End Sub

    Private Sub clearBtn_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles clearBtn.Click
        input1TextBox.Text = ""
        input2TextBox.Text = ""
        answerLbl.Text = ""

        If additionRb.Checked = True Then
            additionRb.Checked = False
            symbolLbl.Text = ""
        ElseIf subtractRb.Checked = True Then
            subtractRb.Checked = False
            symbolLbl.Text = ""
        ElseIf multiplyRb.Checked = True Then
            multiplyRb.Checked = False
            symbolLbl.Text = ""
        Else
            divideRb.Checked = False
            symbolLbl.Text = ""
        End If
    End Sub

End Class

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

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! 

CalculatorinVisualBasic.zip