[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Display locale-aware sample dates and currency values in C#

[Display locale-aware sample dates and currency values in C#]

This program uses the following simple CultureData class to store information about a locale.

// A class to store cultre data. public class CultureData { // The CultureInfo. public CultureInfo Info; // Initializing constructor. public CultureData(CultureInfo info) { Info = info; } // Return the name. public override string ToString() { return Info.EnglishName + "\t" + Info.NativeName; } }

This class's main purpose is to hold a CultureInfo object. It provides an initializing constructor and overrides its ToString method to display the culture's English and native names separated by a tab character.

When it starts, this program uses the following code to display a list of the computer's defined locales.

// List the locales. private void Form1_Load(object sender, EventArgs e) { // Make the ListBox use tabs. lstLocales.UseTabStops = true; lstLocales.UseCustomTabOffsets = true; // Define the tabs. ListBox.IntegerCollection offsets = lstLocales.CustomTabOffsets; offsets.Add(200); // Add the locale information. // Add the locale information. foreach (CultureInfo info in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) lstLocales.Items.Add(new CultureData(info)); // Select the first culture. lstLocales.SelectedIndex = 0; }

This code sets tab stops in a ListBox. It then loops through the values returned by the CultureInfo.GetCultures method and adds a new CultureData object for each locale to the ListBox.

The code passes the method the parameter CultureTypes.SpecificCultures so the method returns only specific locales, not neutral locales. For example, it returns English (Australia) but not English (English). It does this because neutral cultures such as English (English) can't be used in formatting, so you can't use them for a thread's current culture.

After it populates the ListBox, the program selects the first locale in the list.

When you select a locale from the ListBox, the following code displays a sample date and currency value.

// Display date and currency samples. private void lstLocales_SelectedIndexChanged(object sender, EventArgs e) { // Get the selected culture's data. CultureData data = lstLocales.SelectedItem as CultureData; // Set the thread culture and UI culture. Thread.CurrentThread.CurrentUICulture = data.Info; Thread.CurrentThread.CurrentCulture = data.Info; // Display the date sample. lblDate.Text = DateTime.Now.ToString("D"); // Display the date currency value. decimal value = 12345.67m; lblCurrency.Text = value.ToString("C"); }

This code gets the CultureData object that you selected and sets the thread's CurrentUICulture and CurrentCulture properties equal to its CultureInfo object. It then displays a date and currency value in labels. It uses the standard format specifiers D (long date) and C (currency) to format the output.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.