The My.Resources Object






The My.Resources Object

We discussed this in earlier chapters, but as a reminder, you can access an application's resources through the My.Resources object. If you have a string resource named MainFormCaption, the following reference returns its value:

My.Resources.MainFormCaption

All resources are strongly typed. In this case, MainFormCaption is of type System.String. The SplashImage image resource included in the Library Project is declared as type System.Drawing.Bitmap. Because each resource is strongly typed, you can use the My.Resources reference in your code just like any data of the resource's type.

In new Windows Forms applications, all application-wide resources appear in the Resources.resx file, found in the My Project directory within the application's source code directory. You can view it in Notepad if you want. It's a pretty big XML file that doesn't immediately interest me, except that it works! Here's the portion of the Library Project's Resources.resx file that specifies our two existing resources. (I've wrapped some of the lines to make it fit on the page.) I've highlighted the name of each resource, and their strong data types.

<data name="ItemLookupBody"
  type="System.Resources.ResXfileref, System.Windows.Forms">
  <value>..\Resources\ItemLookupBody.txt;System.String,
    mscorlib, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="SplashImage"
  type="System.Resources.ResXfileref, System.Windows.Forms">
  <value>..\Resources\SplashImage.jpg;System.Drawing.Bitmap,
    System.Drawing, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b03f5f7f11d50a3a</value>
</data>

Each form you add to your project also has its own private resource file. The one for Form1 is called Form1.resx. These files end up being a big plus in the localization of Windows Forms applications.

Behind the scenes, your application is taking an object-oriented approach to resource management. It's using the System.Resources.ResourceManager class to locate and return instances of each resource when you need it. And this same class makes decisions about which language-specific or culture-specific resources, from the dozens I'm sure you'll have added to your application, will be made visible to the user.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows