Windows Forms Resources

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 Exisitng 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 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.5 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.6 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.7 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.8 How do I embed a manifest file into my exe?

A Win32 resource must be added to your .exe. The type of the resource must be "RT_MANIFEST" and the resource id must be "1". An easy way to do this is with Visual Studio.NET:

1. Open your exe in VS (file -> open file)
2. Right click on it and select add resource
3. Click "Import..." from the dialog
4. Select your manifest file
5. In the "Resource Type" field, enter "RT_MANIFEST"
6. In the property grid, change the resource ID from "101" to "1".
7. Save the exe.

From Mike Harsh at gotnetdot.com.

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 culturely 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-06 Copyright George Shepherd.

17. Windows Forms Scrolling FAQ Home

17.1 How do I add support for custom scrolling to my own user control?

17.2 Are there any events that get fired when the user scrolls?



17.1 How do I add support for custom scrolling to my own user control?

Windows Forms features a ScrollableControl. This will work in most cases where you know the exact dimensions of your control and scroll by pixel. See the MSDN Documentation for ScrollableControl for discussion how to use this control.

Sometimes you may need more customized scrolling, for example if you implemented a text editor and you want to scroll lines and not pixels.

For more customized scrolling you have to use PInvoke to acces the Win32 ScrollWindow method. The following code shows how to enable access to the Win32 ScrollWindow method from your code.

[StructLayout(LayoutKind.Sequential)]

public struct RECT

{

public int left;

public int top;

public int right;

public int bottom;

public RECT(Rectangle rect)

{

this.bottom = rect.Bottom;

this.left = rect.Left;

this.right = rect.Right;

this.top = rect.Top;

}

public RECT(int left, int top, int right, int bottom)

{

this.bottom = bottom;

this.left = left;

this.right = right;

this.top = top;

}

}

[DllImport("user32")]

public static extern bool ScrollWindow(IntPtr hWnd, int nXAmount, int nYAmount, ref RECT rectScrollRegion, ref RECT rectClip);

void MyScrollFunc(int yAmount)

{

RECT r = new RECT(ClientRectangle);

ScrollWindow(Handle, 0, yAmount, ref r, ref r);

}

17.2 Are there any events that get fired when the user scrolls?

You could override WndProc and listen for (0x114 WM_HSCROLL) and (0x115 WM_VSCROLL) messages (m.Msg will be set to the above values). These messages should be sent when the user scrolls.

No comments: