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


Other Windows

Visual Basic has a number of other windows you will use during program development. They are not shown in Figure 2.2 because Visual Basic displays these windows only when you need them. For the most part, we’ll deal with these other windows as the need arises. One type of window, however, is so central to Visual Basic programming that I’ll take a moment to introduce it now. In a code window, you enter and edit Basic code. It works pretty much like any other Windows text editor: You can type text, delete it, move and copy it from place to place, and so on. If you want to see what a code window looks like, click on the View Code button in the Project Explorer window. An empty code window will open.


TIP:  Using Visual Basic Windows

Visual Basic’s main window is like any other Windows window—you can change its size and position, minimize it to an icon, and so on. Its subwindows are even more clever. As you resize the main window, the subwindows all stretch or shrink to fit. If you point to the border between two subwindows, you can drag it to adjust the window sizes, making one larger and one smaller. Click on the X in the title bar of a subwindow to close it; select the corresponding command from the View menu to display the subwindow again.


A Visual Basic Overview

Now that you’ve had a look at the most important parts of Visual Basic, you may be wondering how they all fit together. With Visual Basic, more than with other programming tools, understanding how all the parts fit together is essential if you are going to realize its full potential. We went over the most important points in the previous chapter, but let’s go over them again briefly before beginning your first Visual Basic project.

  A Visual Basic program consists of one or more windows, or forms; each form contains a number of controls. A wide variety of different controls is available, providing just about any functionality your program could need—entering and editing text, selecting options, displaying graphics, and so on. Together, the forms and controls make up a program’s visual interface.
  Each object (form or control) in a Visual Basic program has a number of properties associated with it. An object’s properties control the way it behaves and looks. Properties can be modified by the programmer during program design and by Basic code as the program is executing.
  Visual Basic objects have the ability to detect events, such as mouse clicks and key presses. This ability is built in and requires no effort on your part. What happens when an event is detected, however, is up to the programmer.
  Basic code written by the programmer defines the functionality of the program—in other words, what the program does. Whether it’s text processing, graphical display, or numerical calculations, the programmer’s job is to write the code to perform the desired actions. Basic code also serves to link the program’s visual interface to the program’s functionality—to control what happens in response to those events that Visual Basic objects can automatically detect.

Your First Visual Basic Program

All right, enough talk. It’s time to dive in and get your fingers dirty. We’re going to create a real, live Visual Basic program—and not just some silly demonstration program, but a real program capable of doing something useful. We’ll write a mortgage calculator that will display the monthly payment on a mortgage or other loan. It won’t be the world’s fanciest program (not even close), but it is a good start to learning Visual Basic.

You will create this program in four steps. These are the same steps you would use for any Visual Basic project.

Step 1: Planning Ahead

Programming projects always benefit from a bit of planning, and Visual Basic is no different. What will a mortgage calculator require? Put on your thinking cap, and get out your paper and pencil. (Yes, even in this computerized age, paper and pencil are still the ideal tools for some tasks.) A few moments of thought yields the conclusion that the program will need to do three things: Gather input information from the user, perform the calculations, and display the answer.

Let’s start with the input. We need three pieces of information to perform the calculations: the amount, or principal, of the loan; the interest rate being charged; and the duration, or term, of the loan. Right away, we know the program will need places for the user to enter these three items. One of Visual Basic’s controls, the Text Box, is intended for entering and displaying information of this sort. At the completion of this first planning step, we know the project will need three Text Box controls for input.

As for output, our program will generate only one piece of information—the monthly loan payment. Again, a Text Box control is ideal for this purpose, so we’ll add one output Text Box to the three input Text Boxes, for a total of four. Note that these are all the same type of Text Box control; designating them as input and output reflects only the way the program will use them.

We also need some way to identify the Text Box controls to determine which one is for the interest rate, which is for the loan term, and which is for the amount. The Label control is ideal for this purpose, and we’ll need one for each input Text Box.

Finally, we need some way for users to quit the program. Yes, we could let them quit by using the default window controls and clicking the X button in the upper right corner. For a little more elegance, let’s provide a Quit button instead. Visual Basic’s Command Button control is suited for such a task, and we’ll need only one.

Our planning is now complete. Well, almost. We also need to know how to calculate the mortgage payment when given the amount, term, and rate. Fortunately, I happen to have a Handbook of Financial Formulas on my shelf. If we define the following:

rate = interest rate per period

amount = loan amount

nper = term of loan in periods

then:

monthly payment = (amount * rate) / (1 - (1 + rate) ^ -nper)

Because the formula is written in Basic, it may not be clear to you. You’ll learn all about “Basic-speak” in later chapters. For now, all you need to know is that * means multiply, / means divide, and ^ means “raised to the power of.” In this formula, all items must be expressed in the same units. In other words, if you want to calculate monthly payments, you have to enter the monthly interest rate and the loan term in months.

Okay, now the planning is really complete. Of course, your plan is not engraved in stone. The freedom to make changes later is one of the beauties of Visual Basic. But now, it’s time to get to work.

Step 2: Designing The Interface

The next step in creating a Visual Basic program is usually the design of the visual interface. This means we are going to start with a blank form and place the needed controls on it. Because we were wise enough to plan ahead, we already know what controls will be on the form. All we need to do now is decide on the visual layout—where the controls are located, what size they are, and so on.


Previous Table of Contents Next


Pro