392
LESSON 34 LocaLizing Programs
TRY IT
In this Try It, you write the program shown in Figures 34-5 and 34-6, which let you select fore-
ground and background colors in American English and Mexican Spanish.
FIGURE 345
FIGURE 346
You can download the code and resources for this Try It from the book’s web
page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find
them in the Lesson34 folder of the download.
Lesson Requirements
Build the default interface.
Add code to handle the
RadioButtonsClick events.
Localize the application for Mexican Spanish.
Add code to let you test the form for either locale.
Hints
There’s no need to build a separate event handler for each
RadioButton. Use one event handler
for all of the foreground buttons and one for all of the background buttons.
These event handlers must then figure out which button was clicked, but they cannot use the
buttons’ text because that will change depending on which locale is selected. They could use
the buttons’ names because they don’t change, but it’s even easier to store the corresponding
colors’ names in their
Tag properties and then use the Color class’s FromName method to get
the appropriate
Color.
Step-by-Step
Build the default interface.
1. Build a form that looks like the one shown in Figure 34-5.
596906c34.indd 392 4/7/10 12:34:47 PM
Try It
393
2. Store the color names (red, green, blue, and so forth) in the radio buttons’ Tag properties.
Add code to handle the
RadioButtonsClick events.
1. Write an event handler similar to the following. The code converts the sender object
into a
RadioButton and uses its Tag property to get the appropriate color. It then
applies that color to the form and the two
GroupBoxes.
// Set the foreground color.
private void Foreground_Click(object sender, EventArgs e)
{
// Get the sender as a RadioButton.
RadioButton rad = sender as RadioButton;
// Use the color.
Color clr = Color.FromName(rad.Tag.ToString());
this.ForeColor = clr;
fgGroupBox.ForeColor = clr;
bgGroupBox.ForeColor = clr;
}
2. Connect the foreground RadioButtons to this event handler.
3. Repeat these steps for the background RadioButtons.
Localize the application for Mexican Spanish.
1. Set the form’s Localizable property to true. Click the Language property, click the
dropdown arrow to the right, and select “Spanish (Mexico).”
2. Change the controls’ Text properties so they have the values shown in Figure 34-6.
Add code to let you test the form for either locale.
1. Use code similar to the following in the form’s constructor:
// Select a locale for testing.
public Form1()
{
// English.
//Thread.CurrentThread.CurrentCulture =
// new CultureInfo(“en-US”, false);
//Thread.CurrentThread.CurrentUICulture =
// new CultureInfo(“en-US”, false);
// Spanish.
Thread.CurrentThread.CurrentCulture =
new CultureInfo(“es-MX”, false);
Thread.CurrentThread.CurrentUICulture =
new CultureInfo(“es-MX”, false);
InitializeComponent();
}
Please select Lesson 34 on the DVD to view the video that accompanies this lesson.
596906c34.indd 393 4/7/10 12:34:47 PM
394
LESSON 34 LocaLizing Programs
EXERCISES
1. Copy this lesson’s Try It and add support for Italian
(it-IT) as shown in Figure 34-7. Don’t forget to add
code to let you test it.
2. When a program reads data from a file, it must use
the correct locale. Download the files Dutch.txt,
German.txt, and English.txt from the book’s web
site and make a program that can read them. The
program should let the user select a file, check the file-
name to see which locale it should use, and select the
correct locale. It should read and parse the values into
appropriate data types and then display the values
again in a
DataGridView control.
Hint: Use locale names en-US for English, de-DE for German, and nl-NL for Dutch. Use
code similar to the following to select the proper locale before you parse the values:
Thread.CurrentThread.CurrentCulture =
new CultureInfo(“en-US”, false);
Hint: The values within a line in the file are separated by tabs so use File.ReadAllLines to
get the lines and
Split to break each line into fields.
The following text shows the values in the file Dutch.txt:
Potlood 0,10 12 1,20
Blocnote 1,10 10 11,00
Laptop 1.239,99 1 1.239,99
You can download the solutions to these exercises from the book’s web page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find those
solutions in the Lesson34 folder.
FIGURE 347
596906c34.indd 394 4/7/10 12:34:48 PM
Click here to Play
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.107.32