Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
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

Bookmark It

Search this book:
 
Previous Table of Contents Next


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 (it’s the left button at the top of the window). Visual Basic will open a code editing window, as shown in Figure 2.7. (Don’t worry about any code that may be displayed in this window; we don’t 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. Don’t worry about the options in the dialog box. We’ll 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, you’ll see how Basic procedures are central to Visual Basic programming. For now, we are concentrating on finishing the project—explanations 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:


Figure 2.7  A code editing window.


Figure 2.8  The Add Procedure dialog box.

  Text that you type appears at the cursor (the blinking vertical line).
  Use the arrow keys to move the cursor.
  Press Enter to start a new line.
  Use Backspace to delete characters to the left of the cursor.

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 isn’t finished yet!” That’s 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, though—no answer appears in the Monthly Payment box. Also, nothing happens if you click on the Exit button (you’ll 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. We’ve written the code to perform the program’s calculations, and we’ve created the visual interface. The missing link is the connection between the interface and the code. This is our next task.


Note:  You’ll find yourself switching between viewing objects and viewing code quite often. Visual Basic gives you several ways to do it: (1) click on the View Code or View Object button in the Project Explorer window; (2) press F7 or Shift+F7 to view code or object, respectively; or (3) select Code or Object from the View menu.

Step 4: Connecting The Code To The Interface

When I describe this step as “connecting the code to the interface,” it may sound rather technical. It’s 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 didn’t 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 events—to things the user does. So exactly what events are we interested in? Let’s deal with them one at a time.

Exiting The Program

Let’s take the Exit button first. The event of interest, of course, is when the user clicks on the button. We want:

  To write code that causes the program to terminate.
  To cause that code to be executed when the user clicks on the button.


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.