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

Simple Calculator in (VB) Visual Basic with Source Code

aditya158

Big O Analyzer
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
70
Likes
152
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
This is a Sample calculator that I created in visual basic 2008, and this sample will be more useful for learners who are interested in VB 2008 and fortunately the VS 2019 can upgrade the source code perfectly. You can study it and modify it as more as you like.

How to Build a Simple Calculator

First, create an Interface for your calculator which has the following:

  • TextField
  • Buttons for the numbers
  • Buttons for the Operators
  • Button for the Result
  • Off, Clear, and Backspace Button

In my Case, this is the interface I used.
calculator.png

After that, set the variables for the operator, value 1, value 2, and other needed for operations.

  1. Option

    Explicit

    On

  2. Public

    Class Form1
  3. Dim

    Operand1 As

    Double
  4. Dim

    Operand2 As

    Double
  5. Dim

    [Operator] As

    String

  6. Dim

    hasDecimal As

    Boolean
  7. Dim

    tmpValue As

    Double
  8. End

    Class

Then, for each number buttons. For script below, my textfield named as "txtInput" and "cmd1" for my number 1 button.

  1. Private

    Sub

    cmd1_Click(ByVal

    sender As

    System.Object

    , ByVal

    e As

    System.EventArgs) Handles cmd1.Click
  2. txtInput.Text = txtInput.Text & sender.text
  3. End

    Sub

For the operation buttons except for the square root button, follow the script below and change the "[Operator]" value according to the operator you have set to the button.

  1. Private

    Sub

    cmdAdd_Click(ByVal

    sender As

    System.Object

    , ByVal

    e As

    System.EventArgs) Handles cmdAdd.Click
  2. Operand1 = Val(txtInput.Text)
  3. txtInput.Text = ""
  4. txtInput.Focus()
  5. [Operator] = "+"
  6. End

    Sub

And for the equal/result button. Use the following code below.

  1. Private

    Sub

    Button23_Click(ByVal

    sender As

    System.Object

    , ByVal

    e As

    System.EventArgs) Handles Button23.Click
  2. Dim

    Result As

    Double
  3. Operand2 = Val(txtInput.Text)

  4. Select

    Case

    [Operator]
  5. Case

    "+"
  6. Result = Operand1 + Operand2
  7. txtInput.Text = Result.ToString()
  8. Case

    "-"
  9. Result = Operand1 - Operand2
  10. txtInput.Text = Result.ToString()
  11. Case

    "/"
  12. Result = Operand1 / Operand2
  13. txtInput.Text = Result.ToString()
  14. Case

    "*"
  15. Result = Operand1 * Operand2
  16. txtInput.Text = Result.ToString()
  17. Case

    "^"
  18. Result = Operand1 ^ Operand2
  19. txtInput.Text = Result.ToString()
  20. Case

    "%"
  21. Result = Operand1 * 1 / 100
  22. txtInput.Text = Result.ToString()


  23. End

    Select

  24. txtInput.Text = Result.ToString()

  25. End

    Sub

Next is we will create the function that calculates the square root of the value when the square root button will be clicked.

  1. Private

    Sub

    cmdSqrtRoot_Click(ByVal

    sender As

    System.Object

    , ByVal

    e As

    System.EventArgs) Handles cmdSqrtRoot.Click
  2. 'Make sure the input box has a value
  3. If

    txtInput.Text.Length <> 0 Then
  4. 'Assign our variable the value in the input box
  5. tmpValue = CType(txtInput.Text, Double

    )
  6. 'Perform the square root
  7. tmpValue = System.Math.Sqrt(tmpValue)
  8. 'Display the results in the input box
  9. txtInput.Text = CType(tmpValue, String

    )
  10. 'Clear the decimal flag
  11. hasDecimal = False
  12. End

    If

  13. End

    Sub

And lastly, creating a function for the button clear, backspace, and decimal.

  1. 'for the Decimal
  2. Private

    Sub

    cmdDecimal_Click(ByVal

    sender As

    System.Object

    , ByVal

    e As

    System.EventArgs) Handles cmdDecimal.Click
  3. If

    InStr(txtInput.Text, "."

    ) > 0 Then
  4. Exit

    Sub
  5. Else
  6. txtInput.Text = txtInput.Text & "."
  7. End

    If
  8. End

    Sub

  9. 'Clear Function
  10. Private

    Sub

    CmdClearAll_Click(ByVal

    sender As

    System.Object

    , ByVal

    e As

    System.EventArgs) Handles CmdClearAll.Click
  11. txtInput.Text = ""

  12. End

    Sub

  13. 'Off Function
  14. Private

    Sub

    cmdOFF_Click(ByVal

    sender As

    System.Object

    , ByVal

    e As

    System.EventArgs) Handles CmdOff.Click

  15. txtInput.Text = ""

  16. End

    Sub

That's it, you can now try to debug your simple calculator and test if the Source Code works perfectly as planned. In case it doesn't work the way it should be, you download the working source code below.

Happy Coding : )

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

 

452,292

323,341

323,350

Top