Visual Basic Concepts

Adding, Removing, and Saving Files

Working with files within a project is similar to working with the projects themselves.

To add a file to a project

  1. Select Project, Addfiletype (where 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.

Figure 4.2   The Add Form dialog box

When you add a file to a project, you are simply including a reference to the existing file in the project; you are not adding a copy of the file. Therefore, if you make changes to a file and save it, your changes will affect any project that includes the file. To change a file without affecting other projects, select the file in the Project Explorer, choose Save filename As from the File menu, and then save the file under a new file name.

Note   You can drag and drop files from the Windows Explorer, File Manager, or Network Neighborhood into the Project window to add them to a project. You can also drag and drop .ocx files onto the toolbox to add new controls.

To remove a file from a project

  1. Select the file in the Project Explorer.

  2. From the Project menu, choose Remove filename.

  3. The file will be removed from the project but not from the disk.

If you remove a file from a project, Visual Basic updates this information in the project file when you save it. If you delete a file outside of Visual Basic, however, Visual Basic cannot update the project file; therefore, when you open the project, Visual Basic displays an error message warning you that a file is missing.

To save an individual file without saving the project

  1. Select the file in the Project Explorer.

  2. From the File menu, choose Savefilename .

Merging Text

You can also insert existing text from other files into one of your code modules. This is useful for adding a list of constants or for adding snippets of code that you might have saved in text files.

To insert a text file into your code

  1. From the Project window, select the form or module into which you want to insert code.

  2. Choose the View Code button, and move the cursor to the point in the Code Editor where you want to insert code.

  3. From the Edit menu, choose Insert File.

  4. Select the name of the text file you want to insert, and choose Open.

Note   If you edit Visual Basic source files using a text or code editor other than Visual Basic, be careful not to change settings of the attribute VB_PredeclaredId. In particular, changing this attribute may cause serious problems with the GlobalMultiUse and GlobalSingleUse classes.

In general, you should not edit attributes manually, as doing so may put the module into an internally inconsistent state.