home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


The +/- button changes the sign of the displayed number from negative to positive, or from positive to negative. First, we use the Left function to check if the number already begins with a minus sign. To do this, pass this function a string and a number (n), and it returns the leftmost n characters of the string. In this case, we obtain the first character of the displayed text. If it is not “-”, then we know the number is positive and can make it negative by adding a leading “-”. If the number is negative, we can use the Len and Right functions to remove the leading “-” and make the number positive. Len is passed a string and returns its length (the number of characters). Right works just like Left, but returns the rightmost n characters of the string. Thus, the expression

Right(txtDisplay.Text, Len(txtDisplay.Text) - 1)

has the effect of trimming the first character off the display. The full code for this function is presented in Listing 5.5.

Listing 5.5 The +/- button Click event procedure.

Private Sub cmdPlusMinus_Click()
‘ Change the sign of the displayed value.

If (Left(txtDisplay, 1) <> “-”) Then
    txtDisplay.Text = “-” & txtDisplay
Else
    txtDisplay.Text = Right(txtDisplay.Text, _
        Len(txtDisplay.Text) - 1)
End If

End Sub

The Clear button is an easy one. All we need to do is set the Text property of the txtDisplay Text Box to an empty string, set the NewEntry flag, and clear the DisplayHasData flag. The procedure code is given in Listing 5.6.

Listing 5.6 The Clear button Click event procedure.

Private Sub cmdClear_Click()

‘ Clear the display to 0, clear valid data
‘ flag, and set new entry flag.

txtDisplay.Text = “0.00”
NewEntry = True
DisplayHasData = False

End Sub

The Backspace button has the task of removing the rightmost character from the display. If the NewEntry flag is set, we do nothing—we don’t want to erase part of an entered or calculated number. Otherwise, we check to see that the display is not empty. If it isn’t, we use the Left and Len functions to remove the last character from the display. The full event procedure is presented in Listing 5.7.

Listing 5.7 The Backspace button Click event procedure.

Private Sub cmdBackspace_Click()

‘ If we’re in the process of making an
‘ entry, remove the rightmost character
‘ from the display.

If NewEntry Then Exit Sub

If txtDisplay.Text <> “” Then
    txtDisplay.Text = Left(txtDisplay.Text, _
        Len(txtDisplay.Text) - 1)
End If

End Sub

The event procedure for the Decimal button is a bit more complicated. As with the number keys, if a new entry is being started, the event procedure clears the Text Box and clears the NewEntry flag, then checks the entry for the following two conditions:

  If the display is empty (length = 0), the decimal is added as the first character.
  If the display is not empty, we see if a decimal point already has been entered. If not, the decimal is added at the end of the displayed string.

To determine if one string is present in another string, use the Instr function. It takes two arguments: the string to be searched and the string you are looking for. If the string is found, the function returns the position where it was found (with the first character in position 1). If the string is not found, the function returns 0. Here are some examples:

  Instr(“Visual”, “V”) evaluates to 1
  Instr(“Basic”, “q”) evaluates to 0
  Instr(“Programming”, “gr”) evaluates to 4

Now that we know how Instr works, we can complete the event procedure. Complete code for this event procedure is shown in Listing 5.8.

Listing 5.8 The Decimal button Click event procedure.

Private Sub cmdDecimal_Click()

‘ If we’re starting a new entry, clear the text box.
If NewEntry Then
    txtDisplay.Text = “”
    NewEntry = False
End If

‘ If the display is empty add the decimal
‘ point as the first character. If the display
‘ is not empty add the decimal point at the
‘ end of existing text only if there is not
‘ already a decimal point entered.

If Len(txtDisplay.Text) = 0 Then
    txtDisplay.Text = “.”
Else
    If InStr(txtDisplay.Text, “.”) = 0 Then
        txtDisplay.Text = txtDisplay.Text & “.”
    End If
End If

End Sub

Go ahead and run the project. You’ll be able to enter numbers, clear the display, backspace, and so on. Of course, the calculator won’t calculate yet. Our next task is programming the operator buttons.


TIP:  Watching The Stack

By adding a few lines of code to the program, you’ll be able to keep track of the calculator’s stack—a good way to increase your understanding of how the stack works. Use the techniques you learned in the section “Creating a Procedure” to create a new sub procedure called ShowStack, and then add the following code:

Public Sub ShowStack()

‘ If the calculator is running in the VB
‘ environment, displays the top 5 values
‘ on the stack with an arrow to the top
‘ value.

Dim i As Integer

If StackPointer < 1 Then Exit Sub

For i = 5 To 1 Step -1
    If i = StackPointer Then
        Debug.Print i & ": " & Stack(i) & " <<"
    Else
        Debug.Print i & ": " & Stack(i)
    End If
Next i

Debug.Print "=========="

End Sub

To call the procedure each time DisplayResults is called, insert the line

Call ShowStack

just before the End Sub statement in the DisplayResults procedure. In the Debug window, ShowStack displays the first five stack positions with an arrow pointing to the top value (the one pointed to by StackPointer).


Programming The Operator Buttons

Our last task in creating a functioning calculator is to write the event procedure for the operator buttons. Because we added these buttons to the form as a control array, we will need only a single procedure for all four buttons. As before, with the number buttons, we can tell which button was clicked by looking at the Index argument that is passed to the procedure.

The code is actually rather simple. First, we declare a type Double variable named Result to hold the result of the calculation. We must also ensure that the stack has at least one value on it. Then we perform the requested operation b