Resource Files in Windows Forms


Just as with ASP.NET, you can also work with resource files (.resx) right from Visual Studio. To see how to localize a Windows Forms application, create a new form in your Localization project called UsingResx.vb.

Like the ASP.NET form described earlier in this chapter (and identified as “ASP.NET page code block”), this Windows Form dialog contains a couple of Label controls, a Button, and a TextBox control. Ultimately, your form should look like the one shown in Figure 6-18.

image from book
Figure 6-18

Note that the controls were just laid out on the form and no text was added yet. Before you get started, let’s turn on localization features for the form. You can also do this to a form that is already completed if you are later converting a form to deal with more than one language.

Highlighting the form in the designer, change the Localizable property to True. This will enable you to apply more than one language to a form and have the elements for a particular culture stored in a resource file. After you have set the Localizable property to True, you can then provide values to the Label and the Button controls on the form. The properties that you assign to the controls are done under the (Default) language setting. You will find this setting within the Language property of the form, as shown in Figure 6-19.

image from book
Figure 6-19

Setting the text values to the (Default) property setting means that if a culture is undetermined or a resource file isn’t available for this culture, then the default one will be utilized.

From there, change the Language property of the form to Finnish. You can then change the values of three of the controls as follows:

 Button1.Text    Lähetä Nimi Label1.Text     Mikä sinun nimi on? Label2.Text     Hei

Double-clicking on the form’s button will allow you to create a Button1_Click event. The code for this page is as follows:

  Imports System.Globalization Imports System.Threading Public Class UsingResx     Sub New()         Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")         Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-US")         ' This call is required by the Windows Form Designer.         InitializeComponent()     End Sub     Private Sub Button1_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles Button1.Click         Label2.Visible = True         Label2.Text += TextBox1.Text     End Sub End Class 

This assigns the CurrentCulture and the CurrentUICulture properties to en-US; the form it produces is shown in Figure 6-20.

image from book
Figure 6-20

Now change the code so that it works with a Finnish culture setting:

 Imports System.Globalization Imports System.Threading Public Class UsingResx     Sub New()         Thread.CurrentThread.CurrentCulture = New CultureInfo("fi-FI")         Thread.CurrentThread.CurrentUICulture = New CultureInfo("fi-FI")         ' This call is required by the Windows Form Designer.         InitializeComponent()     End Sub     Private Sub Button1_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles Button1.Click         Label2.Visible = True         Label2.Text += TextBox1.Text     End Sub End Class

Running the form with this change in place now produces the window shown in Figure 6-21.

image from book
Figure 6-21

So where are all the translations stored? Just like ASP.NET, they are stored in the resource file for this form. Looking in the Solution Explorer, you will now find a UsingResx.resx and a UsingResx.fi.resx file, as shown in Figure 6-22.

image from book
Figure 6-22

Opening the UsingResx.resx file will cause Visual Studio to open the file in a manner that allows you to directly edit the values it stores. The default resource file stores some type references as well as other properties of the controls on the form, as shown in Figure 6-23.

image from book
Figure 6-23

Opening the UsingResx.fi.resx file instead shows only the three different properties that you changed. The rest of the properties are read from the default resource file. The contents of the Finnish resource file are presented in Figure 6-24.

image from book
Figure 6-24




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

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