Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!:Visual Design + Basic Code = Visual Basic
function GetCookie (name)
{
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg) {
var end = document.cookie.indexOf (";", j);
if (end == -1)
end = document.cookie.length;
return unescape(document.cookie.substring(j, end));
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
var m1='';
var gifstr=GetCookie("UsrType");
if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; }
document.write(m1+m2+m3);
Keyword
Title
Author
ISBN
Publisher
Imprint
Brief
Full
Advanced Search
Search Tips
Please Select
-----------
Components
Content Mgt
Certification
Databases
Enterprise Mgt
Fun/Games
Groupware
Hardware
IBM Redbooks
Intranet Dev
Middleware
Multimedia
Networks
OS
Prod Apps
Programming
Security
UI
Web Services
Webmaster
Y2K
-----------
New Titles
-----------
Free Archive
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
function isIE4()
{
return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') );
}
function bookMarkit()
{
var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0";
parent.location.href=url;
//var win = window.open(url,"myitk");
//if(!isIE4())
// win.focus();
}
Search this book:
Previous
Table of Contents
Next
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.
Figure 5.6 The calculator in action.
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:
Data arrays are a useful way of storing large quantities of data in an indexed fashion. Combining arrays with loops is a powerful technique that youll see frequently in all kinds of Visual Basic programs.
A control array lets you group two or more controls under the same name. By sharing a name and event procedures, control arrays greatly reduce the coding required for certain tasks.
General procedures are used to isolate individual program tasks, reducing coding and debugging effort and producing simpler and more robust programs.
Visual Basics built-in functions perform a variety of commonly needed tasks.
The Debug window and the Debug.Print method provide a useful way for peeking at whats going on inside your program.
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 Calculator
The 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:
Command Button 1
Caption: 1/X
Name: cmdInverse
Command Button 2
Caption: Y^X
Name: cmdPower
Command Button 3
Caption: Last X
Name: cmdLastX
Command Button 4
Caption: Copy
Name: cmdCopy
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.
Figure 5.7 The completed Calculator form.
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
Previous
Table of Contents
Next
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.
Wyszukiwarka
Podobne podstrony:
R 05 07r 05 07mini city PL 05 07Przyklad 8 sup 2014 05 07TI 01 05 07 T pl(2)Log2012 05 07więcej podobnych podstron