RESOURCE FILES

Resource files contain text, images, and other data for the application to load at run time. The intent of resource files is to let you easily replace one set of resources with another.

One of the most common reasons for using resource files is to provide different resources for different languages. To create installation packages for different languages, you simply ship the executable and a resource file that uses the right language. Alternatively, you can ship resource files for all of the languages you support and then let the application pick the appropriate file at run time based on the user’s computer settings.

Resource files are not intended to store application configuration information and settings. They are intended to hold values that you might want to change, but only infrequently. You should store frequently changing data in configuration files or the system registry rather than in resource files.

The distinction is small and frankly somewhat artificial. Both configuration files and resource files store data that you can swap without recompiling the application. Rebuilding resource files can be a little more complex, however, so perhaps the distinction that configuration and setting data changes more frequently makes some sense.

Resource files can also be embedded within a compiled application. In that case, you cannot swap the resource file without recompiling the application. Although this makes embedded resource files less useful for storing frequently changing information, they still give you a convenient place to group resource data within the application. This is particularly useful if several parts of the application must use the same pieces of data. For example, if every form should display the same background image, it makes sense to store the image in a common resource file that they can all use.

The following sections describe the three most common types of resources: application, embedded, and localization.

Application Resources

To create application resources in Visual Basic, open Solution Explorer, double-click the My Project entry, and select the Resources tab. Use the drop-down on the left to select one of the resource categories: Strings, Images, Icons, Audio, Files, or Other. Figure 28-3 shows the application’s Resources tab displaying the application’s images.

FIGURE 28-3: The Resources tab contains images and other resources used by the application.

image

If you double-click an item, Visual Studio opens an appropriate editor. For example, if you double-click a bitmap resource, Visual Studio opens the image in an integrated bitmap editor.

Click the Add Resource drop-down list and select Add Existing File to add a file to the program’s resources. Use the drop-down’s Add New String, Add New Icon, or Add New Text File commands to add new items to the resource file. The drop-down’s New Image item opens a cascading submenu that lets you create new PNG, bitmap, GIF, JPEG, and TIFF images.

Using Application Resources

When you create application resources, Visual Studio automatically generates code that adds strongly typed resource properties to the My.Resources namespace. If you open Solution Explorer and click the Show All Files button, you can see the Resources.Designer.vb file that contains this code. The Solution Explorer path to this file is My Project/Resources.resx/Resources.Designer.vb.

The following code shows the property that Resources.Designer.vb contains to retrieve the Octahedron image resource:

Friend ReadOnly Property Octahedron() As System.Drawing.Bitmap
    Get
        Dim obj As Object =
            ResourceManager.GetObject("Octahedron", resourceCulture)
        Return CType(obj,System.Drawing.Bitmap)
    End Get
End Property

The following code shows how a program can use these My.Resources properties. It sets the lblGreeting control’s Text property to the string returned by the My.Resources.Greeting property. Then it sets the form’s BackgroundImage property to the image resource named Dog.

Private Sub Form1_Load() Handles MyBase.Load
    lblGreeting.Text = My.Resources.Greeting
    Me.BackgroundImage = My.Resources.Dog
End Sub

Because these property procedures are strongly typed, IntelliSense can offer support for them. If you type My.Resources, IntelliSense lists the values defined in the application’s resource file.

Example program UseResources uses similar code to set a label’s text and to display an image.

Embedded Resources

In addition to storing resources in the application’s resource file Resources.resx, you can add other resource files to the application. Open the Project menu and select the Add New Item command. Pick the Resources File template, give the file a meaningful name, and click OK.

After you add a resource file to the project, you can double-click it in Solution Explorer to open it in the resource editor. Then you can add resources to the file exactly as you do for the application’s resource file.

Just as it generates strongly typed properties for application resources, Visual Studio generates similar code for other embedded resource files. You can access these properties by adding the resource file’s name after My.Settings and before the resource name. For example, to get the image resource named Dog from the Images resource file, the program would use My.Settings.Images.Dog.

Example program EmbeddedResources uses the following code to set a Label’s text to the resource named Greeting in the file Strings.resx and to set the form’s background image to the resource named Dog in the file Images.resx:

Public Sub Form1_Load() Handles MyBase.Load
    lblGreeting.Text = My.Resources.Strings.Greeting
    Me.BackgroundImage = My.Resources.Images.Dog
End Sub

Localization Resources

One of the most important reasons for inventing resource files was to allow localization: supporting different text, images, and other items for different languages and cultures. Resources make localization in Visual Studio .NET easy.

First, create a form using whatever language you typically use from day to day. For me, that’s English as spoken in the United States. Open the form in the form designer and give it whatever controls you need. Set the form’s and controls’ properties as usual.

Next, set the form’s Localizable property to True. Then set the form’s Language property to the first language you want to support other than the default language that you have been working with so far. Modify the controls’ properties for the new language.

As you modify a form, Visual Studio saves the changes you make to a new resource file attached to the form. If you open Solution Explorer and click the Show All Files button, you can see these resource files below the form’s file.

Example program Localized uses default settings for United States English. It also includes localizations for generic German (as opposed to German as spoken in Switzerland, Germany, Liechtenstein, or some other country). If you expand the form’s entry in Solution Explorer, you’ll find the files Form1.resx holding the default settings and Form1.de.resx holding the German settings.

At run time, the application automatically checks the user’s computer and selects the best resource file based on the system’s regional settings.

Normally, you should let the application pick the appropriate resource file automatically, but you can explicitly select a resource file for testing purposes. To do that, open the Solution Explorer and click the Show All Files button. Find the form’s design file (for example, Form1.Designer.vb) and open it.

At the top of the file, import the System.Threading and System.Globalization namespaces.

Next, create a parameterless constructor for the form. Add a call to MyBase.New and then set the current thread’s CurrentCulture and CurrentUICulture properties to a CultureInfo object that represents the culture that you want to use.

The LocalizedUseGerman example program uses the following code to select the German localization when it starts:

Imports System.Threading
Imports System.Globalization
 
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form
 
    Public Sub New()
        MyBase.New()
 
        ' Set the culture and UI culture to German.
        Thread.CurrentThread.CurrentCulture = New CultureInfo("de-DE")
        Thread.CurrentThread.CurrentUICulture = New CultureInfo("de-DE")
 
        ' This call is required by the designer.
        InitializeComponent()
 
        ' Add any initialization after the InitializeComponent() call.
 
    End Sub
    ...
End Class

CULTURE COMES FIRST
The program must set the culture and user interface culture before it calls InitializeComponent because InitializeComponent is where the program sets the form and control properties.

The rest is automatic. When the form’s InitializeComponent method executes, it loads the resources it needs for the culture you selected.

Example program LocalizedUseGerman, which is available for download on the book’s website, uses this code to open the form localized for German even if your system would not normally select that version.

For a list of culture codes, see http://msdn.microsoft.com/library/ee825488.aspx.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.107.142