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


The procedure for changing the properties of the other Text Box controls is the same: Click on the Text Box to select it, then change the properties in the Properties window. All of the Text Box controls should be given a blank Text property. Table 2.1 shows the proper values for the Name properties.

We still have one more property to change. While the first three Text Boxes are designed to accept input from the user, the last one—txtMonthlyPayment—does not need to accept user input. To prevent errors, therefore, we will lock it, which means the user cannot change it while the program is running. (The program will still be able to display the answer there, of course.) Click on the Text Box next to the Monthly payment label, then click on the Locked property in the properties window. The property setting in the right column will display False with a small button displaying a downward-pointing arrow. Click on the button to display a list of possible settings for the property; in this case, there are only two: True and False. Click on True to lock the Text Box.

I want to point out two things about the names we assigned to the Text Box controls. Each name consists of two parts: a prefix to identify the type of control and an identifier that describes the function of the control. This is not necessary as far as Visual Basic is concerned—we could have called them Moe, Larry, and Curly—but when you are writing a larger program with lots of controls and code, you will find it useful to be able to tell from the name of a control what type of control it is and what its function is.

Once all of the Text Box controls are complete, one more step will complete the visual interface.

Table 2.1 Proper values for the Text Box Name properties.

For The Text Box Next To This Label Assign This Name Property
Loan period (months): txtLoanPeriod
Loan amount: txtLoanAmount
Monthly payment: txtMonthlyPayment


Note:  Why didn’t we change the Name property of the Label controls? Because the Label controls are not referenced in the program’s code, their names are unimportant, and the default names assigned by Visual Basic (Label1, Label2, and so on) are perfectly adequate.

Adding A Command Button

During the planning stage, we decided that the Mortgage Calculator should have a Quit button allowing the user to exit the program. Visual Basic’s Command Button control is what we need, and we place this on the form in the same manner as the other controls. Click on its icon in the toolbox (the icon is a small gray rectangle, normally located just below the Text Box icon), then drag on the form to place the control. You’ll need to change the Command Button’s Name and Caption properties, too. You already learned how to do this by using the Properties window; set the Name property to cmdExit and the caption property to Exit.

Adjusting The Form Size

If you find that the form’s size is too big or too small for the controls you have placed on it, the problem is easily fixed. All you need do is select the form by clicking on it, then point at one of its dark handles and simply drag the form to the desired size. This might be a good time to fine-tune the position and sizes of the controls as well. Change each control that you want to adjust by dragging it to its new position, or dragging its handles to change its size.

Changing The Form’s Caption

While it’s not strictly necessary for the program to function, we can do one more thing to improve its appearance: We can have the main program window—the form—display the name of the program while it’s running. As you might guess by now, the text displayed in the window’s title bar is controlled by a property. In this case, it happens to be a property of the form, and here’s how to change it:

1.  Click anywhere on the form between the controls. When selected, the Properties window will display “Form1 form” on the first line below its title bar.
2.  Double-click on the Caption property.
3.  Type “Mortgage Calculator”.
4.  Press Enter.

The visual design stage of this project is now complete, and your Visual Basic design screen should look more or less like Figure 2.6. We can now move to the third stage of development: writing the code. But first, saving the project to disk will safeguard our work against your three-year-old tripping over the computer’s power cord or some other unexpected disaster.


Figure 2.6  The completed Mortgage Calculator form.

Saving The Project

As you work on a Visual Basic project, all associated information is stored in your computer’s random access memory (RAM). The problem with RAM is that information is lost when the power is turned off. If you want your project to be there tomorrow, you must save it to disk. If you have any experience with other Windows programs, you probably already know how to do this, but I’ll run through the procedure just in case. This step has two parts: saving the form and saving the project.

Pull down the File menu by clicking on File in the Menu window or by pressing Alt+F. Next, select the Save Project command from the menu by clicking on it or by pressing v. Visual Basic will display the Save File As dialog box, in which you specify a name for the disk file that the form is stored in. In the dialog box’s File Name box, Visual Basic suggests a default file name that is the same as the form’s Name property. Because we have not changed the form’s Name property from its default value of Form1, that’s what Visual Basic will suggest as a file name. We can do much better, however. Type “Mortgage” and press Enter.

Next, Visual Basic displays the Save Project As dialog box. Again, we have a File Name box with a default project name, typically Project1. Type “Mortgage Calculator” and press Enter. If, during this process, you are asked whether you should add the project to SourceSafe, just click on No.

That’s all there is to it. Your project is now saved to disk, and you’ll be able to load and continue working on it the next time you use your computer. Now that you have assigned file names to your project and form, pressing Ctrl+S automatically saves them under the existing names. Do this once in a while as you work on a project—and, of course, when you are finished and ready to quit Visual Basic.

Step 3: Writing The Code

The Mortgage Calculator’s visual interface is complete. You can run the program now by pressing F5, by clicking on the Start button (the right-pointing arrowhead) on the toolbar, or by selecting Start from the Run menu. You’ll see the window you designed with all of its elements in place. You’ll even be able to enter text in the three top Text Box controls. The program does not do anything, however, and you’ll have to click on the Close button in the title bar (the button with the X on it) to end the program and return to Visual Basic.


Previous Table of Contents Next