What Are VB.Net Resources and How Are They Used?

Finger touching a screen covered with icons.

geralt/Pixabay

After Visual Basic students learn all about loops and conditional statements and subroutines, one of the next things that they often ask is, "How do I add a bitmap, a .wav file, a custom cursor, or some other special effect?" One answer is resource files. When you add a resource file to your project, it's integrated for maximum execution speed and minimum hassle when packaging and deploying your application.

Using resource files isn't the only way to include files in a VB project, but it has real advantages. For example, you could include a bitmap in a PictureBox control or use the mciSendString Win32 API. 

Microsoft defines a resource as "any nonexecutable data that is logically deployed with an application."

The easiest way to manage resource files in your project is to select the Resources tab in the project properties. You bring this up by double-clicking My Project in Solution Explorer or in your project properties under the Project menu item.

Types of Resource Files

  • Strings
  • Images 
  • Icons
  • Audio
  • Files
  • Other

Resource Files Simplify Globalization

Using resource files adds another advantage: better globalization. Resources are normally included in your main assembly, but .NET also lets you package resources into satellite assemblies. This way, you accomplish better globalization because you include only the satellite assemblies that are needed. Microsoft gave each language dialect a code. For example, the American dialect of English is indicated by the string "en-US," and the Swiss dialect of French is indicated by "fr-CH." These codes identify the satellite assemblies that contain culture-specific resource files. When an application runs, Windows automatically uses the resources contained in the satellite assembly with the culture determined from Windows settings.

VB.Net Add Resource Files

Because resources are a property of the solution in VB.Net, you access them just like other properties: by name using the My.Resources object. To illustrate, examine this application designed to display icons for Aristotle's four elements: air, earth, fire, and water.

First, you need to add the icons. Select the Resources tab from your Project Properties. Add icons by choosing Add Existing File from the Add Resources drop-down menu. After a resource is added, the new code looks like this:

Private Sub RadioButton1_CheckedChanged( ...
Handles MyBase.Load
Button1.Image = My.Resources.EARTH.ToBitmap
Button1.Text = "Earth"
End Sub

Embedding With Visual Studio

If you're using Visual Studio, you can embed resources directly into your project assembly. These steps add an image directly to your project:

  • Right-click the project in the Solution Explorer. Click Add and then click Add Existing Item.
  • Browse to your image file and click Open.
  • Display the properties for the image that was just added.
  • Set the Build Action property to Embedded Resource.

You can then use the bitmap directly in code like this (where the bitmap was the third one, index number 2 in the assembly).

Dim res() As String = GetType(Form1).Assembly.GetManifestResourceNames()
PictureBox1.Image = New System.Drawing.Bitmap( _
GetType(Form1).Assembly.GetManifestResourceStream(res(2)))

Although these resources are embedded as binary data directly in the main assembly or in satellite assembly files, when you build your project in Visual Studio, they're referenced by an XML-based file format that uses the extension .resx. For example, here's a snippet from the .resx file you just created:

<assembly alias="System.Windows.Forms" name="System.Windows.Forms,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="AIR"
type="System.Resources.ResXFileRef,
System.Windows.Forms">
<value>..\Resources\CLOUD.ICO;System.Drawing.Icon,
System.Drawing, Version=2.0.0.0,
Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a</value>
</data>

Because they are just text XML files, a .resx file can't be used directly by a .NET framework application. It has to be converted to a binary ".resources" file, adding it to your application. This job is accomplished by a utility program named Resgen.exe. You might want to do this to create the satellite assemblies for globalization. You have to run resgen.exe from a command prompt.

Source

"Resources Overview." Microsoft, 2015.

Format
mla apa chicago
Your Citation
Mabbutt, Dan. "What Are VB.Net Resources and How Are They Used?" ThoughtCo, Feb. 16, 2021, thoughtco.com/vbnet-resource-files-3424443. Mabbutt, Dan. (2021, February 16). What Are VB.Net Resources and How Are They Used? Retrieved from https://www.thoughtco.com/vbnet-resource-files-3424443 Mabbutt, Dan. "What Are VB.Net Resources and How Are They Used?" ThoughtCo. https://www.thoughtco.com/vbnet-resource-files-3424443 (accessed March 28, 2024).