Calculator in Visual Basic

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.
  1. Dim curTotal As Integer = 0
  2. Dim curValue As String = Nothing
  3. Dim prevValue As String = Nothing
  4. 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...
  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2. curValue += "1"
  3. End Sub
  4.  
  5. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  6. curValue += "2"
  7. End Sub
  8.  
  9. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  10. curValue += "3"
  11. End Sub
  12.  
  13. Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
  14. curValue += "4"
  15. End Sub
  16.  
  17. Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
  18. curValue += "5"
  19. End Sub
  20.  
  21. Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
  22. curValue += "6"
  23. End Sub
  24.  
  25. Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
  26. curValue += "7"
  27. End Sub
  28.  
  29. Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
  30. curValue += "8"
  31. End Sub
  32.  
  33. Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
  34. curValue += "9"
  35. End Sub
  36.  
  37. Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
  38. curValue += "0"
  39. 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...
  1. Private Function calculate()
  2. Dim toPerform As Integer = Convert.ToInt32(curValue)
  3. If (curAction = 0) Then
  4. 'add
  5. Dim toPerform2 As Integer = toPerform + Convert.ToInt32(prevValue)
  6. curTotal += toPerform2
  7. ElseIf (curAction = 1) Then
  8. 'sub
  9. Dim toPerform2 As Integer = toPerform - Convert.ToInt32(prevValue)
  10. curTotal -= toPerform2
  11. ElseIf (curAction = 2) Then
  12. 'mul
  13. Dim toPerform2 As Integer = toPerform * Convert.ToInt32(prevValue)
  14. curTotal *= toPerform2
  15. ElseIf (curAction = 3) Then
  16. 'div
  17. Dim toPerform2 As Integer = toPerform / Convert.ToInt32(prevValue)
  18. curTotal /= toPerform2
  19. End If
  20. curValue = Nothing
  21. updateBox(curTotal.ToString())
  22. 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...
  1. Private Function updateBox(ByVal show As String)
  2. TextBox1.Text = show
  3. 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'...
  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2. curValue += "1"
  3. updateBox(curValue)
  4. End Sub
  5.  
  6. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  7. curValue += "2"
  8. updateBox(curValue)
  9. End Sub
  10.  
  11. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  12. curValue += "3"
  13. updateBox(curValue)
  14. End Sub
  15.  
  16. Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
  17. curValue += "4"
  18. updateBox(curValue)
  19. End Sub
  20.  
  21. Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
  22. curValue += "5"
  23. updateBox(curValue)
  24. End Sub
  25.  
  26. Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
  27. curValue += "6"
  28. updateBox(curValue)
  29. End Sub
  30.  
  31. Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
  32. curValue += "7"
  33. updateBox(curValue)
  34. End Sub
  35.  
  36. Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
  37. curValue += "8"
  38. updateBox(curValue)
  39. End Sub
  40.  
  41. Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
  42. curValue += "9"
  43. updateBox(curValue)
  44. End Sub
  45.  
  46. Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
  47. curValue += "0"
  48. updateBox(curValue)
  49. 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...
  1. Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
  2. curAction = 0
  3. prevValue = curValue
  4. curValue = ""
  5. updateBox(curValue)
  6. End Sub
  7.  
  8. Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
  9. curAction = 1
  10. prevValue = curValue
  11. curValue = ""
  12. updateBox(curValue)
  13. End Sub
  14.  
  15. Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
  16. curAction = 2
  17. prevValue = curValue
  18. curValue = ""
  19. updateBox(curValue)
  20. End Sub
  21.  
  22. Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
  23. curAction = 3
  24. prevValue = curValue
  25. curValue = ""
  26. updateBox(curValue)
  27. End Sub
Calculate Button Press: Finally we just need to run the calculate method on the calculate button mouse click event...
  1. Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
  2. calculate()
  3. End Sub

Add new comment