Page 170 - DCAP408_WEB_PROGRAMMING
P. 170
Windows Programming
Notes when they're needed. Microsoft has given 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, for example: "en-AU.resx" for Australian English. When an application runs, Windows will
automatically use the resources contained in the satellite assembly with the culture determined
from Windows settings.
Since resources are a property of the solution in VB.NET, you access them just like other properties:
by name, using the My.Resourcesobject. To make this more clear, let's build an application to
display icons for Aristotle's four elements: Air, Earth, Fire and Water.
Step 1 is to add the icons. Select the Resources tab from your project Properties. You can add
icons by either choosing Add Existing File... from the Add Resources drop down menu, or just
drag and drop from a Windows Explorer window.
After a resource has been 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
Although I do my best to keep these articles at a level where VB.NET Express can always be used,
there is another way to use resources that isn't supported in Express. But if you're using Visual
Studio, you can embed them directly in your project assembly. These steps will add an image
directly into to your project.
Right-click the project in the Solution Explorer, click Add, 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 (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 use the extension .resx. For example, here's a snippet from the .resx
file we 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,
164 LOVELY PROFESSIONAL UNIVERSITY