• We just launched and are currently in beta. Join us as we build and grow the community.

Calculator in Visual Basic

kxlemeiring

Compliance Validation Auditor
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
151
Likes
183
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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. Private

    Sub

    Button2_Click(sender As

    Object

    , e As

    EventArgs) Handles Button2.Click
  5. curValue += "2"
  6. End

    Sub

  7. Private

    Sub

    Button3_Click(sender As

    Object

    , e As

    EventArgs) Handles Button3.Click
  8. curValue += "3"
  9. End

    Sub

  10. Private

    Sub

    Button4_Click(sender As

    Object

    , e As

    EventArgs) Handles Button4.Click
  11. curValue += "4"
  12. End

    Sub

  13. Private

    Sub

    Button5_Click(sender As

    Object

    , e As

    EventArgs) Handles Button5.Click
  14. curValue += "5"
  15. End

    Sub

  16. Private

    Sub

    Button6_Click(sender As

    Object

    , e As

    EventArgs) Handles Button6.Click
  17. curValue += "6"
  18. End

    Sub

  19. Private

    Sub

    Button7_Click(sender As

    Object

    , e As

    EventArgs) Handles Button7.Click
  20. curValue += "7"
  21. End

    Sub

  22. Private

    Sub

    Button8_Click(sender As

    Object

    , e As

    EventArgs) Handles Button8.Click
  23. curValue += "8"
  24. End

    Sub

  25. Private

    Sub

    Button9_Click(sender As

    Object

    , e As

    EventArgs) Handles Button9.Click
  26. curValue += "9"
  27. End

    Sub

  28. Private

    Sub

    Button10_Click(sender As

    Object

    , e As

    EventArgs) Handles Button10.Click
  29. curValue += "0"
  30. 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. Private

    Sub

    Button2_Click(sender As

    Object

    , e As

    EventArgs) Handles Button2.Click
  6. curValue += "2"
  7. updateBox(curValue)
  8. End

    Sub

  9. Private

    Sub

    Button3_Click(sender As

    Object

    , e As

    EventArgs) Handles Button3.Click
  10. curValue += "3"
  11. updateBox(curValue)
  12. End

    Sub

  13. Private

    Sub

    Button4_Click(sender As

    Object

    , e As

    EventArgs) Handles Button4.Click
  14. curValue += "4"
  15. updateBox(curValue)
  16. End

    Sub

  17. Private

    Sub

    Button5_Click(sender As

    Object

    , e As

    EventArgs) Handles Button5.Click
  18. curValue += "5"
  19. updateBox(curValue)
  20. End

    Sub

  21. Private

    Sub

    Button6_Click(sender As

    Object

    , e As

    EventArgs) Handles Button6.Click
  22. curValue += "6"
  23. updateBox(curValue)
  24. End

    Sub

  25. Private

    Sub

    Button7_Click(sender As

    Object

    , e As

    EventArgs) Handles Button7.Click
  26. curValue += "7"
  27. updateBox(curValue)
  28. End

    Sub

  29. Private

    Sub

    Button8_Click(sender As

    Object

    , e As

    EventArgs) Handles Button8.Click
  30. curValue += "8"
  31. updateBox(curValue)
  32. End

    Sub

  33. Private

    Sub

    Button9_Click(sender As

    Object

    , e As

    EventArgs) Handles Button9.Click
  34. curValue += "9"
  35. updateBox(curValue)
  36. End

    Sub

  37. Private

    Sub

    Button10_Click(sender As

    Object

    , e As

    EventArgs) Handles Button10.Click
  38. curValue += "0"
  39. updateBox(curValue)
  40. 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. Private

    Sub

    Button12_Click(sender As

    Object

    , e As

    EventArgs) Handles Button12.Click
  8. curAction = 1
  9. prevValue = curValue
  10. curValue = ""
  11. updateBox(curValue)
  12. End

    Sub

  13. Private

    Sub

    Button13_Click(sender As

    Object

    , e As

    EventArgs) Handles Button13.Click
  14. curAction = 2
  15. prevValue = curValue
  16. curValue = ""
  17. updateBox(curValue)
  18. End

    Sub

  19. Private

    Sub

    Button14_Click(sender As

    Object

    , e As

    EventArgs) Handles Button14.Click
  20. curAction = 3
  21. prevValue = curValue
  22. curValue = ""
  23. updateBox(curValue)
  24. 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

 

452,496

331,422

331,430

Top