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

### Language

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.

`Option Explicit On Public Class Form1    Dim Operand1 As Double    Dim Operand2 As Double    Dim [Operator] As String     Dim hasDecimal As Boolean    Dim tmpValue As DoubleEnd Class`

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

`    Private Sub cmd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click        txtInput.Text = txtInput.Text & sender.text    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.

`Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click        Operand1 = Val(txtInput.Text)        txtInput.Text = ""        txtInput.Focus()        [Operator] = "+"    End Sub`

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

` Private Sub Button23_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button23.Click        Dim Result As Double        Operand2 = Val(txtInput.Text)         Select Case [Operator]            Case "+"                Result = Operand1 + Operand2                txtInput.Text = Result.ToString()            Case "-"                Result = Operand1 - Operand2                txtInput.Text = Result.ToString()            Case "/"                Result = Operand1 / Operand2                txtInput.Text = Result.ToString()            Case "*"                Result = Operand1 * Operand2                txtInput.Text = Result.ToString()            Case "^"                Result = Operand1 ^ Operand2                txtInput.Text = Result.ToString()            Case "%"                Result = Operand1 * 1 / 100                txtInput.Text = Result.ToString()          End Select         txtInput.Text = Result.ToString()     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.

`   Private Sub cmdSqrtRoot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSqrtRoot.Click        'Make sure the input box has a value        If txtInput.Text.Length <> 0 Then            'Assign our variable the value in the input box            tmpValue = CType(txtInput.Text, Double)            'Perform the square root            tmpValue = System.Math.Sqrt(tmpValue)            'Display the results in the input box            txtInput.Text = CType(tmpValue, String)            'Clear the decimal flag            hasDecimal = False        End If     End Sub`

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

`'for the Decimal Private Sub cmdDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDecimal.Click        If InStr(txtInput.Text, ".") > 0 Then            Exit Sub        Else            txtInput.Text = txtInput.Text & "."        End If    End Sub 'Clear Function Private Sub CmdClearAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdClearAll.Click        txtInput.Text = "" End Sub 'Off Function Private Sub cmdOFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdOff.Click         txtInput.Text = ""  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

Submitted byAnonymous (not verified)on Thu, 01/17/2013 - 16:20

### c++

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

Submitted byAnonymous (not verified)on Sun, 03/10/2013 - 12:11

### SOFTWARE ENGINEER

NICE LOGICS
Submitted bykcp (not verified)on Wed, 07/31/2013 - 12:06

### visual basic 2008

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