15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Windows forms localization Using Visual studio ❘ 589<br />

public BookOfTheDayForm(string culture)<br />

{<br />

if (!String.IsNullOrEmpty(culture))<br />

{<br />

CultureInfo ci = new CultureInfo(culture);<br />

// set culture for formatting<br />

Thread.CurrentThread.CurrentCulture = ci;<br />

// set culture for resources<br />

Thread.CurrentThread.CurrentUICulture = ci;<br />

}<br />

WelcomeMessage();<br />

}<br />

InitializeComponent();<br />

SetDateAndNumber();<br />

code snippet BookOfTheDay/BookOfTheDayForm.cs<br />

The BookOfTheDayForm is instantiated in the Main() method, which can be found in the file Program.cs.<br />

In this method, you pass the culture string to the BookOfTheDayForm constructor:<br />

[STAThread]<br />

static void Main(string[] args)<br />

{<br />

string culture = String.Empty;<br />

if (args.Length == 1)<br />

{<br />

culture = args[0];<br />

}<br />

}<br />

Application.EnableVisualStyles();<br />

Application.SetCompatibleTextRenderingDefault(false);<br />

Application.Run(new BookOfTheDayForm(culture));<br />

code snippet BookOfTheDay/Program.cs<br />

Now you can start the application by using comm<strong>and</strong>-line options. With the running application, you can<br />

see that the formatting options <strong>and</strong> the resources that were generated from the Windows Forms Designer<br />

show up. Figures 22-10 <strong>and</strong> 22-11 show two localizations in which the application is started with the<br />

comm<strong>and</strong>-line options de-DE <strong>and</strong> fr-FR, respectively.<br />

There is still a problem with the welcome message box: the strings are hard-coded inside the program.<br />

Because these strings are not properties of elements inside the form, the Forms Designer does not extract<br />

XML resources as it does from the properties for Windows controls when changing the Localizable<br />

property of the form. You have to change this code yourself.<br />

figure 22-10 figure 22-11<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!