![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
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 nothingwe dont want to erase part of an entered or calculated number. Otherwise, we check to see that the display is not empty. If it isnt, 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 were 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:
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:
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 were 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. Youll be able to enter numbers, clear the display, backspace, and so on. Of course, the calculator wont calculate yet. Our next task is programming the operator buttons.
Programming The Operator ButtonsOur 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 |