Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleBuild a calculator
Keywordscalculator
CategoriesUtilities
 
The basic idea is to keep a stored value in the variable StoredValue. As the user clicks buttons, the program accumulates thge new value in the visible text box. When the user clicks an operator, the program remembers what operator it is. Then when the user clicks =, the program combines the stored value with the current;y displayed value using the operator.

The following code shows some of the key pieces. See the download for more details.

 
' Add a number to the display.
Private Sub cmdNumber_Click(Index As Integer)
    If NewEntry Then
        txtDisplay.Text = Format$(Index)
        NewEntry = False
    Else
        txtDisplay.Text = _
            txtDisplay.Text & Format$(Index)
    End If
End Sub

' Prepare to perform an operation.
Private Sub cmdOperator_Click(Index As Integer)
    ' Perform the previous operation.
    cmdEquals_Click

    ' Remember this operation.
    Operator = Index

    ' Start a new value.
    NewEntry = True
End Sub

' Calculate the result of the previous operation.
Private Sub cmdEquals_Click()
Dim new_value As Double

    If txtDisplay.Text = "" Then
        new_value = 0
    Else
        new_value = CDbl(txtDisplay.Text)
    End If
    Select Case Operator
        Case opNone
            StoredValue = new_value
        Case opAdd
            StoredValue = StoredValue + new_value
        Case opSubtract
            StoredValue = StoredValue - new_value
        Case opMultiply
            StoredValue = StoredValue * new_value
        Case opDivide
            StoredValue = StoredValue / new_value
    End Select
    Operator = opNone
    NewEntry = True
    txtDisplay.Text = Format$(StoredValue)
End Sub
 
This is a fairly complex application so I have not finished it completely. In particular, I have not thoroughly tested it and you could add other features like parentheses, square roots, expenents, scientific notation, trigonometric functions, etc.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated