# Simple Calculator in (VB) Visual Basic with Source Code

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.

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

1. Option Explicit On
2.
3. Public Class Form1
4.     Dim Operand1 As Double
5.     Dim Operand2 As Double
6.     Dim [Operator] As String
7.
8.     Dim hasDecimal As Boolean
9.     Dim tmpValue As Double
10. 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.
5.         Select Case [Operator]
6.             Case "+"
7.                 Result = Operand1 + Operand2
8.                 txtInput.Text = Result.ToString()
9.             Case "-"
10.                 Result = Operand1 - Operand2
11.                 txtInput.Text = Result.ToString()
12.             Case "/"
13.                 Result = Operand1 / Operand2
14.                 txtInput.Text = Result.ToString()
15.             Case "*"
16.                 Result = Operand1 * Operand2
17.                 txtInput.Text = Result.ToString()
18.             Case "^"
19.                 Result = Operand1 ^ Operand2
20.                 txtInput.Text = Result.ToString()
21.             Case "%"
22.                 Result = Operand1 * 1 / 100
23.                 txtInput.Text = Result.ToString()
24.
25.
26.         End Select
27.
28.         txtInput.Text = Result.ToString()
29.
30.     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.
14.     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.
10. 'Clear Function
11.  Private Sub CmdClearAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdClearAll.Click
12.         txtInput.Text = ""
13.
14. End Sub
15.
16. 'Off Function
17.  Private Sub cmdOFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdOff.Click
18.
19.         txtInput.Text = ""
20.
21.  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.

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

Tags

### c++

paano vah gawin ang">><><<??sample code in c++

NICE LOGICS

### visual basic 2008

`Please Solve To View Comment` 41+72 = ? Please Comment To Get Link