Resources


The .config file, dynamic properties, files in special folders, and isolated storage all provide data used to control an application's look and feel, as well as its behavior, while remaining separate from the code itself. One other major place for this kind of data for applications and controls is resources. A resource is a named piece of data bound into the EXE or DLL at build time. For example, you could set the background image of a form in your application by loading a bitmap from a file:

 public MainForm() {   InitializeComponent();  this.BackgroundImage =   new Bitmap(@"C:\WINDOWS\Web\Wallpaper\Azul.jpg");  } 

The problem with this code, of course, is that not all installations of Windows will have Azul.jpg, and even those that have it may not have it in the same place. Even if you shipped this picture with your application, a space-conscious user might decide to remove it, causing your application to fault. The only safe way to make sure that the picture, or any file, stays with code is to embed it as a resource.

Resources can be conveniently embedded in two ways. One way is to right-click on your project in Solution Explorer, choose Add Existing Item, and open the file you'd like to embed as a resource. The file will be copied into your project's directory but is not yet embedded. To embed the file, right-click on it and choose Properties, changing Build Action from Content (the default) to Embedded Resource. To load the resource, many of the .NET classes, such as Bitmap, provide constructors that take resource identifiers:

 public MainForm() {   InitializeComponent();  this.BackgroundImage =   new Bitmap(this.GetType(), "Azul.jpg");  } 

When embedded as a resource, the name of the resource will be composed of the project's default namespace [4] and the name of the file where the resource came from ”in this example, MySecondApp.Azul.jpg. When the picture is loaded at run time, the first argument is a type that shares the same namespace as that of the embedded resource, and the second argument is the rest of the name.

[4] You can get the default namespace of a C# project by right-clicking on a project in the Solution Explorer and choosing Properties Common Properties General Default Namespace.

Luckily, if the resource-naming scheme is less than intuitive for you or you'd really like to see what the background image is going to look like in the Designer, you can set the value of many properties in your forms by using the Property Browser directly, skipping the need to write the resource-loading code at all. For example, to set the background image for a form, you merely press the " " button in the Property Browser next to the BackgroundImage property and choose the file from the file system; the data will be read directly into a bundle of resources maintained for that form. This action causes the image to be shown in the Designer and the code to be generated that loads the resource at run time:

 namespace MySecondApp {   public class MainForm : System.Windows.Forms.Form {     public MainForm() {       InitializeComponent();     }     private void InitializeComponent() {  System.Resources.ResourceManager resources =   new System.Resources.ResourceManager(typeof(MainForm));   ...   this.BackgroundImage =   (Bitmap)resources.GetObject("$this.BackgroundImage");   ...  }  ...  } 

In this case, instead of using the Bitmap constructor directly, the generated code is using the ResourceManager class. This class will load the bundle of resources specific to this form, whether those resources happen to be in the executing application or in a library assembly with a set of localized resources specific to the current user's culture settings. In this way, you can localize your forms without changing the code or even recompiling. For more details about resources, including localization and internationalization concerns, see Chapter 10: Resources.



Windows Forms Programming in C#
Windows Forms Programming in C#
ISBN: 0321116208
EAN: 2147483647
Year: 2003
Pages: 136
Authors: Chris Sells

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net