Learn the most popular programming language of the most popular Operating System: Windows

Ad

Naming a Project

Each time you begin a new project, the default name, Project1, appears at top of the form.  Unless you change the name, every project you work on will be named Project1.  To change the name, choose Project, Project1 Properties (see Figure 2.2), and type a new project name.  For example, you might enter Project2_1 for this initial project.

Figure 2.2

The Project Properties dialog box is used to change the name of the project from the default, Project1, to a name of your choosing.

Exercise 2.1 Using the Save Project As Command

This exercise asks you to do very little, but it does introduce you to the file naming conventions. Start Visual Basic, and perform the following steps:

  1. Open a Standard EXE new project.
  2. Choose Project, Project1 Properties and change the name of the project to Project2_1. Click OK. This new name should appear on the topmost title bar of Visual Basic.
  3. Choose File, Save Project As. A dialog box appears and asks if you want to save the file as Form1.frm. If you click Cancel, the form is not saved and you have to rebuild it if you need it. Imagine that you spent an hour perfecting a form. Unless you save it, your work will be lost.
  4. Before you click Save, navigate to the location where you want to save the file, and assign a name to the form. Assign a unique name - something other than the default, Form1.frm. You can use the notation F2-1.frm to indicate that this form goes with exercise 2.1 (not very clever, but it works). A more descriptive name would be better.

    If you want to store the form on a diskette placed in the a: drive of the computer, navigate to the drive and type F2-1.frm.

  5. After the form has been saved, a second dialog box appears asking you to assign a name to the project. Again, assign a unique name (not the default, Project1.VBP). You can call it P2-1.VBP to indicate that this project goes with Exercise 2.1. Here too, a descriptive project name would be better.

If you want to save the project on a diskette placed in the a: drive of the computer, navigate to the drive and type P2-1.VBP

The Add File and Remove File Commands

Files can be added to, or removed from, the .VBP project file using the file commands contained on the Project menu. When adding or removing files, keep in mind that you are adding or removing files from a project. The following are two Project menu commands that you will use often in this book:

  • Add File - Choose Project, Add File to open a dialog box (see Figure 2.4) that enables you to navigate the Windows file directory to identify a previously saved file and to add the file to a project, for example, an .FRM (form), .BAS (coded module), .CLS (class modules), or .RES (resource file). This command is especially important when you use a file built for a different project. Simply identify the file name, and click OK.
  • Remove <Name of File> - Choose Project, Remove <Name of File> to remove the file from the current Project that is active (has the focus, a term that is described next).

Other Project menu file commands include Add Form, Add MDI Form, Add Module, and Add Class Module. These enable you to add new types of forms and modules to a project.

The Save File and Save File As Commands

The following are two File menu commands that are used to save a particular file rather than an entire project:

  • Save File - Choose File, Save File to save the form or module that is active (has the focus). If the file has previously been saved, this command updates the file. If the file is new, this command prompts you for the name of the file and, once entered, adds the file to a project when the project file (.VBP file) is next saved.
  • Save File As - Choose File, Save File As to save the current file to a disk. This option always prompts you for the file name. Like the File, Save Project As command, File, Save File As is used to save a file for the first time or to save a copy of an existing file.

You will find file commands most useful when you want to use the same forms and modules in different projects.  There is nothing to prohibit you from using the same form in dozens of projects because each form is saved as a separate file.

Share:

Learning How to Manage Visual Basic Projects

In this section, you learn how to open and save a project, add and delete files from the Project window, give an object focus (further explained in later section “The Concept of Focus”), and use the other File commands provided with Visual Basic.  You will also work through several exercises to rest your knowledge of the concepts presented.

Opening, Saving, Adding and Removing Projects

Whenever you open Visual Basic, you always start a new project.  However, there are File commands that enable you to work with both old and new projects, and File and Project commands for working with old and new files.  The File menu on the menu bar contains six commands designed for managing projects, as follows:

  • New Projects – Choose File, New Project to start a new project.  If you are working on a project, this command prompts you to save the current project.  It then closes the current project to enable you to start a new one.
  • Open Project -  Choose File, Open Project to open a saved project.  Before opening a project, this command asks if you want to save a current project if one exists.
  • Add Project – Choose File, Add Project to open a saved project and add it to another, provided one exists.  A Visual Basic application can contain any number of projects.
  • Remove Project – Choose File, Remove Project to remove the selected project from a project group.
  • Save Project As – Choose File, Save Project As to save the current project to a disk.  This option always prompts you for a project name.  Use this command to save a project for the first time or save a copy of an existing project.  When first saving a project, you are asked whether or not you want to save each form and module associated with a project.

    For each form or module, you can assign a name.  You should name each form with a name other than the default name (such as form1.frm and form2.frm); otherwise, you will design over the form when you start a new project and find it impossible to retrieve the form you thought you had saved.  You should also name each project with a unique name, or else Visual Basic will assign every project the same default name.

  • Save Project – Choose File, Save Project to save the current project to a disk.  this option saves all forms and modules as separate files.  It creates a file with .VBP (Visual Basic Project) extension.  If the project is being saved for the first time, this command prompts you for the project name.  If saved earlier, it updates the .VBP file.

The .VBP file is saved as text or in ANSI (American National Standards Institute) format.  This allows you to inspect its contents with any text editor.  Each project contains one – and only one - .VBP file, which provides a description of the project, showing the names of the stored files and other descriptive information.

Share:

Understanding the Concept of a Visual Basic Project

Every Visual Basic application is defined and saved as a project.  Each project, in turn, consists of a collection of files.  These files can include modules, insertable objects, and a single resource file.  Projects themselves are tracked by a project file, which has a .VBP extension.

Form modules, as discussed earlier are first seen as a windows with a caption, but little else.  From modules contain coded instructions called procedures for the form itself and for the controls and insertable objects placed on the form.  Other modules include standard modules and class modules.  You will learn the differences among these types of modules as you progress.

The tools of the toolbox represent controls and insertable objects.  The toolbox can be made visible or invisible by choosing View, Toolbox.  When visible, the toolbox contains a set of icons – each representing a standard or a custom control.  A Visual Basic project always includes a set of standard controls and can also include custom controls.  Each custom control has an .OCX extension.  In this section you are asked to write some beginning Visual Baisc procedures for forms.  In “Adding Controls and Event Procedures to Form Modules,” you will add procedures for stand controls.

Projects can also include a resource file, which can contain either binary data, such as images and sounds, or string data.  Resource files need to be created by a Windows resource compiler (typically a C or C++ language compiler), or you can use the resource compiler bundled with Visual Basic.

The Files that Make Up a Stanbdard EXE Project

In working with Visual Basic, you are asked to work with different types of files.  Each file type is given a unique file extension, which enables you to identify it.  Visual Basic contains over two dozen types of files, each of which has a different extension.  The Table below shows eight common file types contained within a project, their extensions, and their meanings.

Table 2.1 Common File Types
Type of File Extension Extension
Standard Module .BAS Each coded module is saved as a file
Class Module .CLS Each class module is saved as a file
Form Module .FRM Each form is saved as a file
Binary .FRX Each Icon or Picture property value is saved by Visual Basic in .FRX form
Log .LOG A file for logging load errors.
Resource .RES A project can have a single resource file for storing binary and string data
Project .VBP Each project is saved as a file.
Workspace .VBW Each workspace for a project is saved as a file.

This file notation is used throughout.  When you save a form, it is saved with a .FRM extension, and all event procedures written for that form are also saved in the .FRM file.  Because procedures are not saved as separate files with their own unique extension, they become bound to forms.  When you write and save a coded standard module, it is saved with a .BAS extension.  Coded modules are thus different from event procedures written for forms.  Coded modules are not bound to forms.

The Project Window (Project Explorer) in the Visual Basic Design Environment

Whenever you begin a Visual Basic program, you begin a new project.  Each project can contain several forms, several standard and class coded modules, and custom controls.  The Project window displays a list of all modules assigned to a project.  Whenever you begin a new project, a default Project window is created (see Fiigure 2.1) .  If the window fails to appear or is hidden, choose View, Project Explorer; press Ctrl+R; or click the Project Explorer icon.  In addition, your environment can be set to remove the docking of the Project Explorer.  This setting is controlled by choosing Tools, Options, clicking the Docking tab, and turning off the Project Explorer and Object browser selections.

Figure 2.1: This Project window reveals that Project1 contains a forms folder that is limited to a single form.

Figure 2.1 illustrates a Project window that contains three small icons on the left-hand side.  The leftmost icon enables you to view code, while the middle one enables you to view the underlying object (which in this case is the form).  Forms, the third icon, is important when projects become large and contain several forms or when projects are combined.  With large projects, all forms are placed in one folder, and all modules are placed in a second.  Toggling forms enables you to show or hide all forms for a project that are contained in the Forms folder.

Share:

Popular Posts

Search This Blog

Powered by Blogger.

Featured Post

Coded Statements and Methods

In the preceding procedure , you wrote several coded instructions, which introduced two different programming statements and a method: the D...

Recent Posts