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 (.NET).
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.
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 .NET 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. 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 | - String for text files. - Bitmap for image files. - Icon for icon files. - UnmanagedMemoryStream for sound 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 1
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.
Sub SetFormTitle()
Me.Text = My.Resources.Form1Title
End Sub
Example 2
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
Example 3
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
Example 4
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
Example 5
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. 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