Add, delete and save files
Working with multiple files in a project is similar to working with the project itself.
To add files to your project, follow these steps:
1. Select "Project" and "Add filetype" (here, filetype is the type of file). The "Add filetype" dialog box (Figure 4.2) is displayed.
2. Select an existing file or a new file type and choose Open. When you add a file to a project, you simply add a reference to the existing file into the project; you do not add a copy of the file. Therefore, if you change a file and save it, the changes will affect any projects that contain this file. If you want to change a file without affecting other projects, you should select the file in the "Project Explorer", select "Save filename as" from the "File" menu, and then save the file with a new file name.
Note that you can add files to a project by dragging them from Windows Explorer, File Manager, or Network Places and into the Project window. You can also drag .ocx files and drop them into the toolbox to add new controls.
To delete files from the project, follow these steps:
1. Select the file in "Project Explorer".
2. From the Project menu, select Delete filename.
3. This file will be deleted from the project, but will still exist on the disk. If the file is deleted from the project, Visual Basic updates this information in the project file when the project is saved. However, if a file is deleted outside of Visual Basic, Visual Basic cannot update the project file; therefore, when the project is opened, Visual Basic displays an error message warning that a file is missing.
To save only the file without saving the project, follow these steps:
1. Select this file in the "Project Explorer".
2. From the File menu, choose Save filename.
Merge text
It is also possible to insert existing text from other files into a code module. This feature is useful when adding constant lists or adding code snippets that may be saved in text files.
To insert a text file into code, follow these steps:
1. From the "Project" window, select the form or module into which you want to insert code.
2. Select the "View Code" button and move the cursor to the place where you want to insert the code in the code editor.
3. From the Edit menu, choose Insert File.
4. Select the text file name to be inserted and select "Open".
NOTE If you use a text or code editor other than Visual Basic to edit Visual Basic code, be careful not to change the setting of the VB_PRedeclaredId property. In particular, note that changing this attribute may cause serious problems for the GlobalMultiUse and GlobalSingleUse classes.
In general, do not edit these properties by hand, as doing so will leave the module in an internally inconsistent state.
Add controls to the project
The set of controls available in the toolbox can be defined individually for each project. Any given control must be in the toolbox before it can be added to the project's form. The basic set of standard controls that often appear in toolboxes is described in Chapter 3, "Forms, Controls, and Menus."
Add ActiveX controls to the project
ActiveX controls and pluggable objects can be added to the project after they have been added to the toolbox.
To add a control to the project's toolbox, execute:
1. Select Components from the Project menu. The "Parts" dialog box is displayed, as shown in Figure 4.3. The items listed in this dialog box include all registered ActiveX controls, pluggable objects, and ActiveX designers.
2. To add a control (with an .ocx file extension) or insertable object to the toolbox, select the checkbox to the left of the control name.
To view controls with an .ocx file extension, select the Controls tab. To view insertable objects, such as Microsoft Excel charts, select the Insertable Objects tab.
3. Select "OK" to close the "Parts" dialog box. All selected ActiveX controls will now appear in the toolbox.
To add an ActiveX control to the Components dialog box, select the Browse button and search other directories for files with an .ocx extension. Visual Basic automatically selects this check box when adding an ActiveX control to the list of available controls.
Note that each ActiveX control has a file with an .oca extension. This file stores cached library information and other data specific to the control. Files with the .oca extension are usually stored in the same directory as the ActiveX control and can be rebuilt when needed (file size and date can change).
Remove the control from the project
To remove a control from your project, follow these steps:
1. From the Project menu, select Components.
Displays the Parts dialog box.
2. Clear the check box next to each control you want to delete.
The icons for these controls will be removed from the toolbox.
Note that if an instance of a control is used by any form in this project, the control cannot be deleted from the toolbox.
Use objects from other applications
You can also use objects from other applications, such as those contained in the Microsoft Excel object library, either as controls in a toolbox or as programmable objects in code. To add objects to the toolbox, see "Adding controls to your project" earlier in this chapter.
To make objects from another application available in your code, but not as controls, set a reference to that application's object library.
To add a reference to another application's object library, follow these steps:
1. From the Project menu, choose References.
The Reference dialog box is displayed, as shown in Figure 4.4.
2. Select the checkbox next to each reference you want to add to the project.
To add a reference to an application that is not listed in the References dialog box, choose the Browse button, and then select the application.
3. Select "OK" to add the selected reference to the project.
If you are not using any object in the reference library, you should clear the check box for that reference to minimize the number of object references that Visual Basic must resolve, which can reduce the time it takes to compile your project.
Once you have set up a reference to the desired object library, choose Object Browser from the View menu to find a specific object and its methods and properties in the Object Browser. Any object listed in the Object Browser can be used in code.
For more information about the Object Browser, see "Finding Object Information" in Chapter 9, "Programming with Objects."
Use resource files
Resource files allow all text and bitmaps for a specific version used by an application to be collected in one place. It can contain constant declarations, icons, screen text, and other material that may change between localized versions or between various revisions or special configurations.
To add files to your project, follow these steps:
1. From the "Project" menu, select "Add Files".
The Add File dialog box is displayed.
2. Select an existing resource file (.res) and select "Open".
A project can only have one resource file; if you add a second file with a .res extension, an error will occur.
Details For more information about the contents of resource files, see Chapter 16, "International Distribution".
Make and run executable files
Using the procedure below, you can make an executable file (.exe) from Visual Basic.
To make an executable file in Visual Basic, follow these steps:
1. Select "Make projectname.exe" from the "File" menu, where projectname is the application name of the project.
2. To overwrite an existing executable file with a new version, type the file name or browse the directory and select an existing file name.
3. Click the "Options" button to specify some details about the specific version of the executable file in the "Project Properties" dialog box.
4. If you want to modify the version number of the project, you must set the appropriate "major version number", "minor version number" and "revision number". If "Automatic upgrade" is selected, the "revision number" will be automatically increased each time the "make projectname.exe" command of the project is run.
5. To give the application a new name, under Application, type the new name into the Title box. If you want to specify a new icon, select one from the list.
6. By selecting a topic from the list box and entering information in the text box, you can also enter version-specific comments (comments, company name, trademark and copyright information, etc.) for various versions under the "Version Information" box.
7. Select "OK", close the "Project Properties" dialog box, and then select "OK" in the "Make appname.exe" dialog box to compile and connect the executable file. Double-click the executable file's icon to run the executable file as you would any other Windows-based application.
Note that building an executable file from the command line in a DOS session is useful if you want to compile a project in a planned manner. Key in the batch file: Vb6/makeprojectname[.vbp][exename]
For projectname, type the project file name. The variable exename is used to rename the resulting executable file.
conditional compilation
Conditional compilation allows selective compilation of certain parts of a program. Special features of a program can be incorporated into different versions, for example filters for date and currency display should be changed for different language versions of an application.
For more information about conditional compilation, see "Using Conditional Compilation" in Chapter 8, "Programming Again."
Set project options
Visual Basic allows you to customize each project by setting some properties. Use the "Project Properties" dialog box to set it through the "Project Properties" command on the "Project" menu. Property settings are saved in the project file (.vbp).
The following table lists some of the options that can be set:
Many other options are available, including compilation, assembly, and multi-threading. If you want to access some of the more advanced options, you can find more information in the online help.
Details To learn about environment option settings that affect all projects, see Chapter 2, "Developing Applications with Visual Basic."
->