Visual Basic Code Of Simple Calculator



  1. Visual Basic Simple Calculator Code
  2. Visual Basic Calculator Project

To get more sample codes, please get a copy of this book.

Open your Visual Studio or Visual C# Express Edition and create a new project. Set its type to Windows Forms Application and set its name to CalculatorApp. You should see the main form on the workspace as follows: Step 2 Change the form text property to Calculator, because we don’t want our application to have the title Form1 when. Creating a Calculator Visual Studio C#: This Instrucable will guide you through creating a basic calculator application in C# using Microsoft's Visual Studo development software. Visual Studio is a form of development software made by Microsoft to help developers create programs more eas.

Price: $25.00

AUTHOR:DR. LIEW VOON KIONG

Dear Friend,

Visual Basic Samples Code Edition 2 is the Latest E-Book authored by the webmaster of our Visual Basic Tutorials, Dr.Liew.

The sample programs in this book were developed using Visual Basic 6. However, they can be easily modified to build applications for VB.Net. Visual Basic 6 is a third-generation event-driven programming language first released by Microsoft in 1991. In Visual Basic 6, the sky's the limit. You can develop all kinds of applications, including educational apps, financial apps, games, multimedia apps, animations, database applications and more. Visual Basic 6 Sample Code comprises 290 pages of captivating content and 48 fascinating sample codes. All the examples are explained in great detail using easy-to-understand language and illustrated with gorgeous Images. By reading the book and using the sample source codes, you will master Visual Basic programming effortlessly! You will be able to:

  • Understand basic to intermediate concepts of Visual Basic programming.
  • Create your own Visual Basic 6 programs from scratch.
  • Get programming ideas from 48 interesting sample programs.
  • Modify the source codes easily to suit your needs.
You need PayPal account to order this E-book. If you don't have PayPal Account, get your PayPal account by clicking the banner below:

*Please wait for 10 seconds for PayPal to redirect you to E-book Download Page after ordering and making payment.

Mode of Delivery: Download

Warmest Regards

(Dr.Liew)

Disclaimer

Visual Basic Code Of Simple Calculator

This site and the products and services offered on this site are not associated, affiliated, endorsed, or sponsored by Microsoft, nor have they been reviewed tested or certified by Microsoft.

Contact Information

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.

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
3.Else
4.txtInput.Text = cmd1.Text
5.blnInputStatus = True
6.End If

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.”
6.Else
7.txtInput.Text += “.”
8.End If
9.
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”
7.Else
8.‘Call the CalculateTotals method.
9.CalculateTotals()
10.End If
11.
12.‘Assign a value to the function flag.
13.strCalculate = “Add”
14.
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
2.CalculateTotals()
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”
5.Else
6.CalculateTotals()
7.End If
8.strCalculate = “Exponential”
9.blnDecimalPoint = False
10.End If

Visual Basic Simple Calculator Code

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
5.
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.

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

Visual Basic Calculator Project

Next, the code for the Backspace button.

1.‘Declare locals needed.
2.Dim str As String
3.Dim intNumbers As Integer = txtInput.Text.Length
4.
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
10.
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
3.Case“Add”
4.dblValue1 += dblValue2
5.Case“Subtract”
6.dblValue1 -= dblValue2
7.Case“Multiply”
8.dblValue1 *= dblValue2
9.Case“Divide”
10.If dblValue2 = 0 Then
11.MsgBox(“You cannot divide by 0!”, 0, “WARNING”)
12.Exit Sub
13.End If
14.dblValue1 /= dblValue2
15.Case“Exponential”
16.dblValue1 ^= dblValue2
17.End Select
18.txtInput.Text = CType(dblValue1, String)
19.blnInputStatus = False

_____________________________________________________________________________________________
Above is the code for this calculator program.

!PROBLEM!
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?