03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

684 ❘ APPENDIX A Solutions to Exercises<br />

5. Example program ConfigLabel uses the following code to make its label use the font specified<br />

in the config file.<br />

private void Form1_Load(object sender, EventArgs e)<br />

{<br />

greetingsLabel.Font = Properties.Settings.Default.GreetingFont;<br />

}<br />

(Alternatively, you could bind the label’s font to the dynamic setting. Then you wouldn’t<br />

need to use code to set it at run time.)<br />

If you modify the config file and run the compiled executable, you see the new font. In fact, if<br />

you run the program from Visual Studio, you still see the new font because the modified config<br />

file is in the same directory as the executable being run by Visual Studio.<br />

If you delete the config file and run the compiled executable, you get the original font back.<br />

Next, if you run the program from Visual Studio, you get the original font and Visual<br />

Studio replaces the original config file.<br />

6. Example program LocalizedStrings uses the following code to do this.<br />

public Form1()<br />

{<br />

// Set the culture and UI culture to French.<br />

Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");<br />

Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");<br />

}<br />

InitializeComponent();<br />

// Display a greeting.<br />

private void Form1_Load(object sender, EventArgs e)<br />

{<br />

MessageBox.Show(MyStrings.Greeting);<br />

}<br />

When the program loads the fr-FR locale, it displays the message Salut. Visual Studio creates a<br />

subdirectory named fr and puts the localized resource file LocalizedStrings.resources.dll in it.<br />

7. Example program ShowCurrency uses the following code to do this.<br />

private void Form1_Load(object sender, EventArgs e)<br />

{<br />

DateTime date = DateTime.Now;<br />

decimal amount = 12345.67m;<br />

string[] cultures =<br />

{ "fr-FR", "de-DE", "de-CH", "es-MX", "es-ES", "en-US", "en-GB" };<br />

}<br />

foreach (string culture in cultures)<br />

{<br />

Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);<br />

resultsListBox.Items.Add(culture + "\t" +<br />

date.ToShortDateString() + "\t" +<br />

amount.ToString("C"));<br />

}<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!