Introduction:
This tutorial is on how to create a calculator in Visual Basic.
Design:
For this, we want the following;
A textbox named 'textbox1' to show the current output.
Four buttons for add, subtract, multiply and divide.
Ten buttons for the ten digits.
One button for the calculate process.
Variables:
Now we want to create some variable. First we create a curTotal integer which will hold the current total as an integer, and next we want a curAction which is also an integer but holds whether the next sum should be;
0 - Add
1 - Subtract
2 - Multiply
3 - Divide
Finally we also want a string variable named 'curValue' which will hold the current number to add/subtract/multiply or divide the curTotal to/from/by and a 'prevValue' to hold the one to append on to.
Dim curTotal As Integer = 0
Dim curValue As String = Nothing
Dim prevValue As String = Nothing
Dim curAction As Integer = -1
Note:
If you want your calculator to be more accurate, use double, float or long instead of integers.
Number Buttons:
Now for each of the number buttons click we want to add as a string the numeric value of the button to the end of the 'curValue' variable value. So for number 1 button we'll do 1, 2 is 2, 3 is 3, etc, etc...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
curValue += "1"
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
curValue += "2"
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
curValue += "3"
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
curValue += "4"
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
curValue += "5"
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
curValue += "6"
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
curValue += "7"
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
curValue += "8"
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
curValue += "9"
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
curValue += "0"
End Sub
Calculate Process:
Once the calculate button is clicked, this process will be ran. First we get the variable 'curValue's value (as a string), convert it to a 32 integer and then perform the appropriate action to the total ('curTotal'). Remember back to the beginning of the article where I mentioned the exact integer and action pairs of the 'curAction' variable...
Private Function calculate()
Dim toPerform As Integer = Convert.ToInt32(curValue)
If (curAction = 0) Then
'add
Dim toPerform2 As Integer = toPerform + Convert.ToInt32(prevValue)
curTotal += toPerform2
ElseIf (curAction = 1) Then
'sub
Dim toPerform2 As Integer = toPerform - Convert.ToInt32(prevValue)
curTotal -= toPerform2
ElseIf (curAction = 2) Then
'mul
Dim toPerform2 As Integer = toPerform * Convert.ToInt32(prevValue)
curTotal *= toPerform2
ElseIf (curAction = 3) Then
'div
Dim toPerform2 As Integer = toPerform / Convert.ToInt32(prevValue)
curTotal /= toPerform2
End If
curValue = Nothing
updateBox(curTotal.ToString())
End Function
As you can see, this function then runs another function named 'updateBox()' which will simply just set the text of the textbox to the string parsed to it...
Private Function updateBox(ByVal show As String)
TextBox1.Text = show
End Function
We can also do this for everytime the number buttons are clicked so the user can see the value of the variable 'curValue'...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
curValue += "1"
updateBox(curValue)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
curValue += "2"
updateBox(curValue)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
curValue += "3"
updateBox(curValue)
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
curValue += "4"
updateBox(curValue)
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
curValue += "5"
updateBox(curValue)
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
curValue += "6"
updateBox(curValue)
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
curValue += "7"
updateBox(curValue)
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
curValue += "8"
updateBox(curValue)
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
curValue += "9"
updateBox(curValue)
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
curValue += "0"
updateBox(curValue)
End Sub
Action Buttons:
Next we want to set the 'curAction' variables value to the relevant integer to which action button is clicked. 0=add, 1=subtract, 2=multiply, 3=divide. We also set prevValue to curValue and make curValue ready to accept a new input to perform the action with, followed by resetting the textbox using our previously created 'updateBox' function...
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
curAction = 0
prevValue = curValue
curValue = ""
updateBox(curValue)
End Sub
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
curAction = 1
prevValue = curValue
curValue = ""
updateBox(curValue)
End Sub
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
curAction = 2
prevValue = curValue
curValue = ""
updateBox(curValue)
End Sub
Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
curAction = 3
prevValue = curValue
curValue = ""
updateBox(curValue)
End Sub
Calculate Button Press:
Finally we just need to run the calculate method on the calculate button mouse click event...
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
calculate()
End Sub