![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
Our next step, therefore, is to write the code that does the actual work of the project. In this case, the work consists of retrieving the input data that the user has entered in the three input Text Boxes, calculating the corresponding mortgage payment, and displaying the result in the fourth Text Box. Also, we want the program to terminate when the user clicks on the Quit button. The code required to retrieve the input data and perform the calculation is relatively simple, requiring only a dozen or so lines, as you can see in Listing 2.1. Not all readers will be familiar with Basic code. I will, of course, delve into the details of Basic code later in the book. For now, the object is for you to catch a quick glimpse of a working Visual Basic program. Just trust me on the code and type it as shown in the listing. Listing 2.1 The Calculate procedure. Public Sub Calculate() Dim answer As Currency, rate As Single Dim months As Integer, amount As Single If Val(txtInterestRate.Text) > 0 Then If Val(txtLoanPeriod.Text) > 0 Then If Val(txtLoanAmount.Text) > 0 Then rate = Val(txtInterestRate.Text) / 12 months = Val(txtLoanPeriod.Text) amount = Val(txtLoanAmount.Text) answer = (amount * rate) / (1 - (1 + rate) ^ -months) txtMonthlyPayment.Text = Format(answer, Currency) End If End If End If End Sub The first step in adding the code is to switch Visual Basic from form design mode to code editing mode. You can do this in two ways: by double clicking anywhere on the Mortgage Calculator form, or by clicking on the View Code button in the Project Explorer window (its the left button at the top of the window). Visual Basic will open a code editing window, as shown in Figure 2.7. (Dont worry about any code that may be displayed in this window; we dont need it.) Next, display the Tools menu and select Add Procedure. Visual Basic displays the Add Procedure dialog box, which is shown in Figure 2.8. In the dialog box, type Calculate in the Name box, then press Enter. Dont worry about the options in the dialog box. Well use the default settings. Visual Basic adds the following two code statements to the code editing window: Public Sub Calculate() End Sub These two statements define the beginning and end of a Basic procedure, a discrete section of code that has been assigned a name (in this case, the name is Calculate). In later chapters, youll see how Basic procedures are central to Visual Basic programming. For now, we are concentrating on finishing the projectexplanations will follow. Type the remaining code from Listing 2.1 into the code editing window. Be careful with spelling and remember not to duplicate the first and last lines of code. The code editing window is used in the same way as a word processor. Here are the basics:
Once the code has been entered, you should press Ctrl+S to save the additions to the project. The Mortgage Calculator is now ready to take for a spin. You may be thinking, Hold on, it isnt finished yet! Thats absolutely correct, but running the program now will illustrate an important point about Visual Basic programming. To run the program, press F5 or select Start from the Run menu. The program will start and display its dialog box. You can move the blinking cursor between Text Boxes by pressing Tab or Shift+Tab or by clicking on the desired box with the mouse. You can also enter numbers in the three input Text Boxes, just like a real Windows program. (Well, of course, it is a real Windows program.) Funny thing, thoughno answer appears in the Monthly Payment box. Also, nothing happens if you click on the Exit button (youll have to press Alt+F4 or click on the X button in the title bar to exit the program). What could be wrong? Nothing is wrong, but something is missing. Weve written the code to perform the programs calculations, and weve created the visual interface. The missing link is the connection between the interface and the code. This is our next task.
Step 4: Connecting The Code To The InterfaceWhen I describe this step as connecting the code to the interface, it may sound rather technical. Its not, though; all it means is enabling the program to respond to the user. Think about what happened when you ran the incomplete Mortgage Calculator. The program ran, but didnt respond to your input. Whether you entered numbers in the input boxes or clicked on the Exit button, the program just sat there like a bump on a log. We need the program to respond to eventsto things the user does. So exactly what events are we interested in? Lets deal with them one at a time. Exiting The Program Lets take the Exit button first. The event of interest, of course, is when the user clicks on the button. We want:
|
![]() |
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. |