My.Resources Object

Provides properties and classes for accessing the application's resources.

Remarks

The My.Resources object provides access to the application's resources and lets you dynamically retrieve resources for your application. For more information, see Managing Application Resources.

The My.Resources object exposes only global resources. It does not provide access to resource files associated with forms. You must access the form resources from the form. For more information, see Walkthrough: Localizing Windows Forms.

You can access the application's culture-specific resource files from the My.Resources object. By default, the My.Resources object looks up resources from the resource file that matches the culture in the UICulture property. However, you can override this behavior and specify a particular culture to use for the resources. For more information, see Resources in Desktop Apps.

Properties

The properties of the My.Resources object provide read-only access to your application's resources. To add or remove resources, use the Project Designer. For more information, see How to: Add or Remove Resources. You can access resources added through the Project Designer by using My.Resources.resourceName.

You can also add or remove resource files by selecting your project in Solution Explorer and clicking Add New Item or Add Existing Item from the Project menu. You can access resources added in this manner by using My.Resources.resourceFileName.resourceName.

Each resource has a name, category, and value, and these resource settings determine how the property to access the resource appears in the My.Resources object. For resources added in the Project Designer:

  • The name determines the name of the property,

  • The resource data is the value of the property,

  • The category determines the type of the property:

Category

Property data type

Strings

String

Images

Bitmap

Icons

Icon

Audio

UnmanagedMemoryStream

The UnmanagedMemoryStream class derives from the Stream class, so it can be used with methods that take streams, such as the Play method.

Files

Other

Determined by the information in the designer's Type column.

Classes

The My.Resources object exposes each resource file as a class with shared properties. The class name is the same as the name of the resource file. As described in the previous section, the resources in a resource file are exposed as properties in the class.

Example

This example sets the title of a form to the string resource named Form1Title in the application resource file. For the example to work, the application must have a string named Form1Title in its resource file. For more information, see How to: Add or Remove Resources.

Sub SetFormTitle()
    Me.Text = My.Resources.Form1Title
End Sub

This example sets the icon of the form to the icon named Form1Icon that is stored in the application's resource file. For the example to work, the application must have an icon named Form1Icon in its resource file.

Sub SetFormIcon()
    Me.Icon = My.Resources.Form1Icon
End Sub

This example sets the background image of a form to the image resource named Form1Background, which is in the application resource file. For this example to work, the application must have an image resource named Form1Background in its resource file.

Sub SetFormBackgroundImage()
    Me.BackgroundImage = My.Resources.Form1Background
End Sub

This example plays the sound that is stored as an audio resource named Form1Greeting in the application's resource file. For the example to work, the application must have an audio resource named Form1Greeting in its resource file. The My.Computer.Audio.Play method is available only for Windows Forms applications.

Sub PlayFormGreeting()
    My.Computer.Audio.Play(My.Resources.Form1Greeting, 
        AudioPlayMode.Background)
End Sub

This example retrieves the French-culture version of a string resource of the application. The resource is named Message. To change the culture that the My.Resources object uses, the example uses ChangeUICulture.

For this example to work, the application must have a string named Message in its resource file, and the application should have the French-culture version of that resource file, Resources.fr-FR.resx. For more information, see How to: Add or Remove Resources. If the application does not have the French-culture version of the resource file, the My.Resource object retrieves the resource from the default-culture resource file.

Sub ShowLocalizedMessage()
    Dim culture As String = My.Application.UICulture.Name
    My.Application.ChangeUICulture("fr-FR")
    MsgBox(My.Resources.Message)
    My.Application.ChangeUICulture(culture)
End Sub

See Also

Tasks

How to: Add or Remove Resources

Walkthrough: Localizing Windows Forms

Concepts

Resources in Desktop Apps

Other Resources

Managing Application Resources