Condividi tramite


Visual Basic Concepts

Using Resource Files for Localization

A resource file is a useful mechanism for separating localizable information from code in Visual Basic.

Note   You can have only one resource file in your project. If you attempt to add more than one resource file, Visual Basic generates an error message.

Advantages of Storing Strings in Resource Files

When you are writing Visual Basic code, you can use the LoadResString, LoadResPicture, and LoadResData functions in place of references to string literals, pictures, and data. Storing such elements in a resource file offers two benefits:

  • Performance and capacity are increased because strings, bitmaps, icons, and data can be loaded on demand from the resource file, instead of all being loaded at once when a form or a module is loaded.

  • The resources that need to be translated are isolated in one resource file. There is no need to access the source code or recompile the application.

To create a resource file

  1. Select Add New Resource File from the Project menu.

    Note   This command is only available when the Resource Editor Add-In is loaded. To load the Resource Editor Add-In, select Add-In Manager from the Add-Ins menu. In the Add-In Manager dialog box, select VB6 Resource Editor and check the Loaded/Unloaded box.

  2. In the Open A Resource File dialog box, enter a name for the resource file. The resource file will be added to the Related Documents node in the Project Explorer.

Visual Basic recognizes resource files by the .res file name extension. If the resource file does not have the appropriate file name extension, Visual Basic won't load it. Conversely, if any file uses the .res file name extension, Visual Basic interprets that it is a resource file when adding it to the project. If the file does not follow the standard format for a resource file, Visual Basic generates an error message the first time you attempt to use the resource file support functions (LoadResString, LoadResPicture, and LoadResData), or when you try to make an .exe file. Visual Basic will generate the same error message if you try to add a 16-bit resource file to a project.

The .res file, before and after you compile the .exe file, is a standard Windows resource file, which means the resources contained in the file can be loaded in any standard Windows-based resource editor.

To edit a resource file

  1. Select Resource Editor from the Tools menu.

    Note   This command is only available when the Resource Editor Add-In is loaded. To load the Resource Editor Add-In, select Add-In Manager from the Add-Ins menu. In the Add-In Manager dialog box, select VB6 Resource Editor and check the Loaded/Unloaded box.

  2. Select a button from the Resource Editor Toolbar to edit an existing resource or add a new one. To learn more about editing resources, see the Resource Editor Add-In documentation.

Locking Resource Files

Visual Basic uses file locking on the .res file to prevent problems with multiple applications trying to use the file at the same time. Visual Basic will lock the .res file whenever:

  • Visual Basic is in run mode or break mode.

  • You create an .exe file.

For More Information   For an example of how a resource file can be used to create an application that works in several locales, see "The Automated Teller Machine Sample Application" later in this chapter. For background information about programming with resource files, see "Working with Resource Files" in "More About Programming."