Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Analyzing Sample Applications

Perhaps the best way to begin learning about VB application creation is to analyze sample applications. You will gain practice working within the development environment and master the various windows quickly.

To begin, perform these steps:

  1. Start Visual Basic. If the New Project dialog box appears, close it; you will open a project without the dialog box's help.
  2. Select File | Open Project (a toolbar button does this, too) to display folders from which to choose. Select the drive and directory containing the source code you retrieved from the CD that accompanies this book. You will see various project names.
  3. Locate the project with the filename Lesson 9 Int Proj.vbp (the extension may not appear if you've turned off the display of filename extensions in Windows Explorer). The project describes a simple interest rate application that you'll learn how to create in Hour 9, "Combining Code and Controls."

Several files other than those you see in the dialog box actually reside in the Source folder. Visual Basic knows, however, that when you select File | Open Project, you want to open a project and not another kind of file, so Visual Basic displays only project files in the dialog box. All project files end with the .vbp filename extension to distinguish them from form files that end with the .frm extension and module files that end with .bas (other Visual Basic file types exist, but we won't cover them here).

The advantage that projects present to the Visual Basic programmer is that a project is a bookkeeping record of the entire collection of files that compose an application. When you write even a simple Visual Basic application, Visual Basic creates a project for that application so that as the application's functionality grows, the project can track all the files related to that project. No matter how many files go with a project, when you open the project, Visual Basic puts the project's files at your fingertips in the Project window.

Double-click the Lesson 9 Int Proj.vbp project file now. The Project window now displays two folders, one named Forms and one named Modules. Open the Forms folder and then double-click the form named frmInterest in the Project Explorer window. Your development environment changes dramatically to display the project, as shown in Figure 2.2.

02fig02.jpg

Figure 2.2 The Lesson 9 Int Prog project.

As you might guess from the name of the project and from the window in front of you, this application calculates interest rates for loans and investments. Click any plus signs you see in the Project window to expand the list. You now know that this application is a collection of two files: a form and a module file. Actually, a third file goes with the project: the Lesson 9 Int Proj.vbp project file itself, which Visual Basic keeps track of and updates as needed. (Drag the edge of the Project window to see more of the text within the window.)

The Project window describes two important aspects of a VB project: the external disk drive filenames for each project file and the internal names used inside the project. For example, the current form open in the Form window is named frmInterest. VB's title bar shows the name of the open form file and also shows that the file is a form and not another kind of file.

To every area of your project, the form is called frmInterest. This form got its name from the program writer, who named the form frmInterest. As you'll see throughout this tutorial, programmers often precede items they name with a three-letter abbreviation for the object. Thereafter, when you look through a list of object names, you'll know just from the names what the names represent.

The form isn't stored on the disk drive under the name frmInterest, however. As the Project window's parentheses show, the form named frmInterest is called Lesson 9 Int Form.frm on the disk. Rarely will the actual filename match that of the internal name used in the project. In a way, the three-letter abbreviated prefix works like a file extension in that the prefix describes the object type.

The naming rules for internal Visual Basic names differ from those of files. Therefore, you will need to give each object you create in the Project window both a filename and an internal VB name. If you don't supply a name, Visual Basic supplies one for you, but VB isn't good at assigning names to objects. VB would be happy calling every command button on your form Command1, Command2, and so on. When you name command buttons, however, you'll make up names that better match their meaning, such as cmdOK and cmdExit.

Share ThisShare This

Informit Network