02 06 CG234EOX4H4EBGY2ZJTDSHDVJDNNOONYNNY46SI




Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!:Visual Basic For Intelligent Folks
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




We accomplish this by creating an event handler, a procedure that executes when the event (the click) occurs. Dealing with user events, such as button clicks, is one of Visual Basic’s strong points, as I mentioned in the previous chapter. You’ll learn more about this soon, but for now I just want to show you how easy it is.
We need to work with the form. If it is hidden behind the code editing window, click on the View Object button in the Project Explorer window to display it. Now, double click on the Exit Command Button control. Visual Basic displays a code editing window containing the following two lines of code:


Private Sub cmdExit_Click()

End Sub


Now, add the following single line of code between those two lines:



End


The End statement is the Basic command to terminate a program. The job of this event-handling procedure can be ascertained from its name. The first part of the name, cmdExit, is the name of the Command Button control (remember, we assigned this name earlier when we placed the Command Button on the form). The second part, Click, is the name of the event that interests us. Thus, we know that the code in this procedure will be executed when the control named cmdExit receives a click event. Note that Visual Basic detects the click automatically. All we need to do is write the code to be executed when the event occurs.
Try running the program (remember, press F5). Now when you click on the Exit button, the program ends, just as it should. The mortgage calculations are still not working, however. We’ll tackle that problem next.

Note:  Visual Basic gives you two ways to view the code associated with an object. Full-module view displays all of the object’s procedures in the code editing window, with a horizontal line separating procedures from each other. Procedure view displays only a single procedure at a time. In procedure view, you can press PgUp and PgDn to scroll other procedures into view. Switch between these two views by clicking on the small buttons at the bottom of the code editing window, just to the left of the horizontal scroll bar.


Performing The Calculations
You may have caught on already that getting the program to perform the mortgage calculation and display the answer will be triggered by some event. But what event? With no Calculate button to click, we can’t use the Click event. (Actually, we could use this method, but the one I have selected is more elegant.) Think for a moment about what the user will be doing; he or she will be entering or changing information in the three input Text Boxes. Can we use this as the event to trigger the program’s calculations? Indeed we can. Text Box controls have a Change event that is automatically triggered whenever the data in the box changes.
Our strategy is now clear: We will write an event-handling procedure for each of the three input Text Box controls. Because we have already written the Calculate procedure that does the work of performing the mortgage calculations and displaying the answer, all that the code in the Change event procedures will need to do is execute the Calculate procedure.
As before, we begin by double-clicking on the control of interest—in this case, the Annual interest rate Text Box control. Visual Basic displays a code editing window containing these lines of code:


Private Sub txtInterestRate_Change()

End Sub


You can see from the event procedure name that it will be executed when the control named txtInterestRate receives a Change event. Add the following line of code between the two existing lines:


Call Calculate


This line says, in effect, “Execute the code in the procedure named Calculate.” After adding the code, you can close the code editing window by clicking on the X button in the top right corner.

We want the program to respond to changes in all three input Text Boxes, so repeat these steps—double clicking on the control and adding the same line of code—on the other two input Text Box controls, txtLoanPeriod and txtLoanAmount. When you’ve finished, press Ctrl+S to save the project.
Trying It Out
Congratulations! You have just completed your first Visual Basic program. Press F5 to run the program. Enter values in the three input Text Boxes. As soon as something appears in each box, the program will display an answer in the Monthly payment box. As you change your input values, the answer automatically changes to provide the correct answer. Click on the Exit button to quit the program.

Remember that the interest rate values must be entered as decimals—.08 for 8 percent, for example. Also, numbers must be entered as plain numbers—150000 and not $150,000, for example. You can have fun seeing just how much the payments on that $500,000 house would be. Note that the Mortgage Calculator is not limited to mortgages. It can be used for any type of loan with a fixed interest rate and regular payments, such as a car loan. Figure 2.9 shows the Mortgage Calculator in action, displaying the monthly payments for a certain Italian sports car that I can’t afford.
What’s Next?
Now that you have joined the ranks of Windows programmers, where do you go from here? There’s a lot more to learn about Visual Basic, of course. This first program has only scratched the surface. I hope that this quick introduction to the power of Visual Basic has whetted your appetite for more. The next two chapters will show you more details of drawing your program’s visual interface and will teach you more about writing Basic code. Then we will tackle a more complicated project to test drive your new Visual Basic skills. If you think that the Mortgage Calculator is cool, wait until you see what else Visual Basic can do.


Figure 2.9.  The Mortgage Calculator in action.




Previous
Table of Contents
Next








Wyszukiwarka

Podobne podstrony:
02 06 Standard bezpiecznej pracy na dachach
TI 02 06 19 T pl(1)
2015 02 06 2017
02 06 Dodawanie i odejmowanie
2006 02 06 Egzamin
Incomings SCORE w Krakowie 02 06 01 2014
egzamin 06 02 06
02 06 Roboty na dachach v1 1
Wyniki egzaminu z Chemii (12 02 06)
TI 02 06 10 T B pl(1)
TI 02 06 29 B pl
TI 02 06 19 T B pl(1)

więcej podobnych podstron