![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
Listing 5.9 The Operator button Click event procedure. Private Sub cmdOperators_Click(Index As Integer) Dim Result As Double See if there is at least 1 value on the stack. We cannot perform an operation otherwise. If StackPointer < 1 Then Exit Sub Select Case Index Case 0 plus Result = Stack(StackPointer) + Val(txtDisplay.Text) Case 1 minus Result = Stack(StackPointer) - Val(txtDisplay.Text) Case 2 multiply Result = Stack(StackPointer) * Val(txtDisplay.Text) Case 3 divide If (Val(txtDisplay.Text) <> 0) Then Result = Stack(StackPointer) / Val(txtDisplay.Text) Else MsgBox Cannot divide by 0! Exit Sub End If End Select StackPointer = StackPointer - 1 NewEntry = True DisplayHasData = True DisplayResult (Result) End Sub The calculator is now complete. (Well, at least its working, as shown in Figure 5.6.) Take it for a spin, and see how it works. We will be adding some enhancements later in the chapter. But, first, lets sit back and think about the miles weve covered. Is That Cool Or What?Well, I certainly think so. Just look at what Visual Basic has allowed us to accomplish. While it may have taken youa Visual Basic programming newcomeran hour or two to complete this calculator project, even a moderately experienced Visual Basic programmer would have knocked it off in 15 to 30 minutes. So you can see that for a very modest expenditure of time, we have a fully functional (albeit basic) calculator with a slick visual interface. Just thinking about how long this would have taken with the old ways of programming makes me sweat. If you are new to programming, you cant really appreciate the incredible gains. Just imagine the world of difference between a horse and buggy and a 400-horse power Jaguar with a wet bar and mink seatsthat should give you some idea. For those who are unfamiliar with any other way of programming, the speed and power of Visual Basic are simply the way programming ought to be. I agree completelyI just wish it had been around 10 years ago.
But enough complaining. Lets take a moment to review what we have accomplished so far. The calculator project helps to reinforce some of the Visual Basic tools and concepts you learned in previous chapters: drawing controls on a form, setting control properties, and writing event procedures. Youve also been introduced to several powerful new techniques. Lets summarize:
At this point, I think youre ready to start experimenting with Visual Basic on your own. Dont give up reading this book, of course, but by combining further readings with experimentation, youll learn more quickly than by doing just one or the other. I recommend that you spend some time browsing through Visual Basics function library; it contains a great deal of useful stuff. If youre feeling ambitious, a good project to try is a calculator that uses algebraic logic. (Hint: Its more difficult than an RPN calculator.) While our new calculator is quite nice, a few more features would make it even better. Well spend the remainder of this chapter polishing our project. Enhancing The CalculatorThe enhancements that I have in mind will add more functionality to the calculator. We would like the ability to calculate the trigonometric functions Sin, Cos, and Tan, as well as the inverse (1/X). The capability of raising a number to a power would also be a valuable addition to the calculator. Another useful capability would be copying a number in the calculators display to the Windows Clipboard for use in other Windows applications. This would permit the user to perform calculations and then paste the answer directly into any application, such as a letter you are writing with Word or WordPerfect. Finally, having a Last X button that displays the top value from the stack would be useful for certain calculations. For the trig calculations, we will use Visual Basics built-in Sin, Cos, and Tan functions. As you may already have guessed, well use a control array of Command Buttons for these three calculations. Using the techniques you learned earlier, create this array of three buttons on the Calculator form. You may need to enlarge the form and rearrange the other buttons to accommodate new buttons, unless you planned ahead better than I did. Assign the array of buttons the Name property cmdTrig and set the Caption properties to Sin, Cos, and Tan. The code for the event procedure is shown in Listing 5.10. Again, you must be sure that the Index property of each button matches the corresponding Case statement in the procedure. After calculating the correct value, all that is required is setting the two flags and calling the DisplayResult function. (I told you we would get a lot of mileage out of this function.) Listing 5.10 The Trigonometric Command Button Click event procedure. Private Sub cmdTrig_Click(Index As Integer) Trigonometric calculations. Dim Result As Double Select Case Index Case 0 Sin Result = Sin(txtDisplay.Text) Case 1 Cos Result = Cos(txtDisplay.Text) Case 2 Tan Result = Tan(txtDisplay.Text) End Select DisplayResult (Result) NewEntry = True DisplayHasData = True End Sub The remaining buttons we will put on the calculator are all single buttons and not part of a control array. Add four more Command Buttons, assigning the Name and Caption properties shown here:
When you have finished adding the buttons, your form will look similar to Figure 5.7. Although I have rearranged some of the buttons, the final layout is up to you. If you find the default font too small, just change the Font property of the new buttons. The event procedure for the Last X button is shown in Listing 5.11. After verifying that the stack has at least one value on it, the value on the top of the stack is displayed and the flagsare set.
Listing 5.11 The Last X button Click event procedure. Private Sub cmdLastX_Click() Display the value at the top of the stack. If StackPointer > 0 Then DisplayValue (Stack(StackPointer)) End If NewEntry = True DisplayHasData = True End Sub The code to calculate the inverse of a number (1/X) is simple. The only possible catch would be if the user tries to invert 0, so well test for that condition and display a message box if it occurs. Otherwise, all thats necessary is to divide 1 by the value in the display, calling DisplayResult to display it. As usual, the NewEntry and DisplayHasData flags need to be set as well. The code for this event procedure is shown in Listing 5.12. Listing 5.12 The 1/X button Click event procedure. Private Sub cmdInverse_Click() Displays the inverse of the number in the display. If txtDisplay.Text = 0 Then MsgBox Cannot take inverse of 0! Exit Sub Else DisplayResult (1 / txtDisplay.Text) NewEntry = True DisplayHasData = True End If End Sub
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |