Visual Basic Code For Simple Calculator


The calculator above was created after I did some work crunching out the code and searching the Internet for solutions where I got stuck. Thanks to the generosity of the programming fraternity, I managed to come up with something to the best of my amateur ability. I still got stuck with one portion of the code, which I will indicate later.

Python Program to Make a Simple Calculator In this example you will learn to create a simple calculator that can add, subtract, multiply or divide depending upon the input from the user. To understand this example, you should have the knowledge of the following Python programming topics. How to Create Calculator in Visual Basic.Net Full TutorialTo Become a Channel member click on the Link below to JOIN:

Following is the full code for this program, and I call it: QuickCalc.


First and foremost, arrange all the necessary command buttons and a textbox onto a Form. For the textbox, I set it to read-only, and text to “0” (zero, not the letter O), named it txtInput.

Next, some essential global declarations.

1.‘Variables to hold operands.
2.Private dblValue1 As Double
3.Private dblValue2 As Double
4.‘Variable to hold temporary values.
5.Private dblTemp As Double
6.‘True if “.” is used, otherwise false.
7.Private blnDecimalPoint As Boolean
8.Private blnInputStatus As Boolean
9.‘Variable to hold Operator.
10.Private strCalculate As String

Next, the code for Number buttons 1 – 9.

1.If (blnInputStatus AndAlso txtInput.Text <> “0”) Or blnDecimalPoint Then
2.txtInput.Text += cmd1.Text
4.txtInput.Text = cmd1.Text
5.blnInputStatus = True
6.End If

Unlock samsung galaxy mini s5570l ipad. The above code is for button number 1. For 2 to 9, simply change the cmd number to the respective button number.

Next, the code for Number button 0.

1.If blnInputStatus Then
2.‘To prevent multiple zeroes from appearing if 0 is the first digit.
3.If txtInput.Text.Length >= 1 AndAlso txtInput.Text <> “0”Then
4.txtInput.Text += cmd0.Text
5.‘This is to allow 0 to be added after the decimal point.
6.ElseIf blnDecimalPoint Then
7.txtInput.Text += cmd0.Text
8.End If
9.End If

Next, the code for Decimal button.

1.‘To make sure that no decimal point has already been input.
2.If Not blnDecimalPoint Then
3.‘For values > 0 but < 1.
4.If txtInput.Text.Length < 1 Then
5.txtInput.Text = “0.”
7.txtInput.Text += “.”
8.End If
10.blnInputStatus = True
11.‘To ensure only one decimal point per calculation.
12.blnDecimalPoint = True
13.End If

Next, the code for the Addition operator.

1.If txtInput.Text.Length <> 0 Then
2.‘Check the value of the function flag.
3.If strCalculate = String.Empty Then
4.‘Assign the value in the input box to the holder.
5.dblValue1 = CType(txtInput.Text, Double)
6.txtInput.Text = “0”
8.‘Call the CalculateTotals method.
10.End If
12.‘Assign a value to the function flag.
13.strCalculate = “Add”
15.‘Toggle the decimal flag.
16.blnDecimalPoint = False
17.End If

The code for the Subtraction, Multiplication, and Division operators are almost identical, except that you need to change the value assigned to the function flag – Subtract, Multiply, Divide – respectively.

Next, the code for Equal button.

1.If txtInput.Text.Length <> 0 Then
3.strCalculate = String.Empty
4.blnDecimalPoint = False
5.End If

Next, the code for the Exponential button.

1.If txtInput.Text.Length <> 0 Then
2.If strCalculate = String.Empty Then
3.dblValue1 = CType(txtInput.Text, Double)
4.txtInput.Text = “0”
7.End If
8.strCalculate = “Exponential”
9.blnDecimalPoint = False
10.End If

Next, the code for the Square Root button.

1.If txtInput.Text < “0”Then
2.MsgBox(“The root of a negative number is undefined!”, 0, “WARNING”)
3.Exit Sub
4.End If
6.If txtInput.Text.Length <> 0 Then
7.dblTemp = CType(txtInput.Text, Double)
8.dblTemp = System.Math.Sqrt(dblTemp)
9.txtInput.Text = CType(dblTemp, String)
10.blnDecimalPoint = False
11.End If

Next, the code for the Reciprocal button.

1.If txtInput.Text.Length <> 0 Then
2.dblTemp = CType(txtInput.Text, Double)
3.dblTemp = 1 / dblTemp
4.txtInput.Text = CType(dblTemp, String)
5.blnDecimalPoint = False
6.End If

Next, the code for the Positive/Negative button.

Simple Calculator In Visual Studio

1.If txtInput.Text.Length <> 0 Then
2.dblTemp = CType(txtInput.Text, Double)
3.dblTemp *= -1
4.txtInput.Text = CType(dblTemp, String)
5.End If

Next, the code for the Clear All (C) button.

1.txtInput.Text = “0”
2.dblValue1 = 0
3.dblValue2 = 0
4.strCalculate = String.Empty
5.blnDecimalPoint = False

Next, the code for the Clear Entry (CE) button.

1.txtInput.Text = “0”
2.blnDecimalPoint = False

Next, the code for the Backspace button.

1.‘Declare locals needed.
2.Dim str As String
3.Dim intNumbers As Integer = txtInput.Text.Length
5.If txtInput.Text.Length = 1 Or (txtInput.Text.Length = 2 AndAlso
6. txtInput.Text.StartsWith(“-“)) Then
7.txtInput.Text = “0”
8.Exit Sub
9.End If
11.If txtInput.Text.Length > 0 Then
12.‘Get the next to last character.
13.str = txtInput.Text.Chars(txtInput.Text.Length – 2)
14.‘Check if it’s a decimal.
15.If str = “.”Then
16.‘If it is, then toggle the blnDecimalPoint flag.
17.blnDecimalPoint = False
18.txtInput.Text = txtInput.Text.Remove(intNumbers – 2)
19.Exit Sub
20.End If
21.txtInput.Text = txtInput.Text.Remove(intNumbers – 1)
22.End If

Finally, the CalculateTotals procedure.

1.dblValue2 = CType(txtInput.Text, Double)
2.Select Case strCalculate
4.dblValue1 += dblValue2
6.dblValue1 -= dblValue2
8.dblValue1 *= dblValue2
10.If dblValue2 = 0 Then
11.MsgBox(“You cannot divide by 0!”, 0, “WARNING”)
12.Exit Sub
13.End If
14.dblValue1 /= dblValue2
16.dblValue1 ^= dblValue2
17.End Select
18.txtInput.Text = CType(dblValue1, String)
19.blnInputStatus = False

Above is the code for this calculator program.

Visual Basic Code For Simple Calculator

However, there is an area which I have some niggling issues with.

1. For the + – * / and ^ operators, if I were to click on one of them multiple times after I input the first value, they will go performing the operation on their own without me entering the second value. How do I stop this from happening?

In this video we will learn creating a very Simple Calculator using Visual Basics .net.

Get the source code from here:

Visual Basic 6.0 Source Code For Simple Calculator

If you want something to be made for you from my side. Please don’t hesitate in telling me on comments.

Cleaning things up and Tab Preparation (Part 5) – [Build an Instagram Clone]

Visual Basic Code For Simple Calculator Tutorial

If you found the video helpful please SUBSCRIBE and SHARE.

Visual Basic 6.0 Code For Simple Calculator

For updates follow us on