George Shepherd's Windows Forms FAQ
Questions and answers in this FAQ have been collected from newsgroup posts, various mailing lists and the employees of Syncfusion.

16. Windows Forms Resources

WinForms FAQ Home
   16.1 What are the steps to compiling and using multiple language resources?
   16.2 How do I load a BMP file that has been added to my solution as an embedded resource?
   16.3 Why doesn't a Form containing an ImageList not open in WinRes?
   16.4 How do I embed a manifest file into my exe?
   16.5 How can I save a temporary disk file from an embedded string resource?
   16.6 How do I read embedded resources?
   16.7 How can I check if a certain resource is included in a assembly?
   16.8 Where can I find information on globalization?
   16.9 How do I read or write resources from code?
   16.10 How do I create resources to make culture-aware programs without re-compiling code?
   16.11 How do I programmatically use resources to make my programs culturally aware?
   16.12 How can I dynamically load a resource file?



16.1 What are the steps to compiling and using multiple language resources?


Localization Sample illustrates how to load different language resources depending upon a parameter passed in when you run the program from a command line. The sample can display English, French, Spanish and German words using the same exe. For example, typing Test de on the command line will count to three in German. Typing Test fr will count to three in French. The argument passed in is used to determine the culture setting for the resources.

In each folder is a batch file that handles compiling the single exe and multiple resources. The tools RESGEN and AL are used in these batch files to generate the resources and resource DLL's. Notice the naming conventions used by the subfolders containing the foreign resource DLLs.


16.2 How do I load a BMP file that has been added to my solution as an embedded resource?


If you add a BMP file to your solution using the File|Add Existing Item... Menu Item, then change the Build Action property of this BMP file to Embedded Resource, you can then access this resource with code similar to:

     
     // WindowsApplication6 corresponds to Default Namespace in your project settings.
     // subfolders should be the folder names if any, inside which the bmp is added. If the bmp was added to the top level, you don't have to specify anything here.
     string bmpName = "WindowsApplication6.subfolders.sync.bmp";
     System.IO.Stream strm = null;
     try
     {
          strm = this.GetType().Assembly.GetManifestResourceStream(bmpName);
          pictureBox1.Image = new Bitmap(strm);
          // Do the same for Icons
          // Icon icon1 = new Icon(strm);
     }
     catch(Exception e)
     {
          MessageBox.Show(e.Message);
     }
     finally
     {
          if(strm != null)
               strm.Close();
     }


16.3 Why doesn't a Form containing an ImageList not open in WinRes?


There seems to be a bug in the WinRes utility (1.0) that prevents a form from getting loaded in the WinRes editor (usually an exception like "Object reference not found" occurs). A quick workaround is to always name your ImageLists to be of the form "imageListN" (case sensitive; where N is the smallest no. you could use without a clash with other ImageLists).


16.4 How do I embed a manifest file into my exe?


This can be done through the SendKeys class in the System.Windows.Forms namespace. Check it out in the MS help documentation.


16.5 How can I save a temporary disk file from an embedded string resource?


//usage: string s = CopyResourceToTempFile(GetType(), "showcase.txt");
//where showcase.txt is an embedded resource

static string CopyResourceToTempFile(Type type, string name)
{
     string temp = "";
     string nsdot = type.Namespace;
     if (nsdot == null)
          nsdot = "";
     else if (nsdot != "")
          nsdot += ".";
     Stream stream = type.Module.Assembly.GetManifestResourceStream(nsdot + name);
     if (stream != null)
     {
          StreamReader sr = new StreamReader(stream);
          temp = Path.GetTempFileName();
          StreamWriter sw = new StreamWriter(temp, false);
          sw.Write(sr.ReadToEnd());
          sw.Flush();
          sw.Close();
     }
     return temp;
}


16.6 How do I read embedded resources?


Check out the entry titled How do I load a BMP file that has been added to my solution as an embedded resource? in this section. That code can be used for any kind of embedded resource.


16.7 How can I check if a certain resource is included in a assembly?


If a resource is not available in a assembly or in satellite assemblies you will get a message like this "Could not find any resources appropriate for the specified culture...". You can open your assembly and any related satellite assemblies in ILDASM (tool that ships with the .NET framework). When your assembly is open in ILDASM take a look at the manifest file. This contains a list of the resources in your assembly. Usually such errors are the result of incorrect namespaces being used or from spelling errors.


16.8 Where can I find information on globalization?


Here are some MSDN globalization samples:
Visual Basic .NET Code Sample: Working with Resource Files
.NET Samples - How To: Globalization and NLS

Also look at the following topic in online help inside IDE.
Developing World-Ready Applications


16.9 How do I read or write resources from code?


You can use the ResourceWriter object to create resource files.

Take a look at the discussion and code found on gotdotnet.com


16.10 How do I create resources to make culture-aware programs without re-compiling code?


Placing culture dependent resources in a separate file from your compiled program code allows you to write applications that can change cultures without having to recompile code to handle different resources.

Take a look at the discussion and code found on gotdotnet.com


16.11 How do I programmatically use resources to make my programs culturally aware?


You can use the ResourceManager object to dynamically load a particular resource based on a selected culture. This technique allows you to develop culture-aware programs without having to recompile your application.

Take a look at the discussion and code found on gotdotnet.com


16.12 How can I dynamically load a resource file?


You use the ResourceManager class found in System.Resources to access and control resources.

See the article by Caspar Boekhoudt on C# Corner for a detailed discussion.

© 2001-2010 Copyright Syncfusion Inc. All rights reserved.  |  Privacy Policy  |  Contact  |  Sitemap