Setting the Culture and UI Culture for Windows Forms Globalization
The two culture values of a Visual Basic or Visual C# application determine what resources are loaded for an application and how information like currency, numbers, and dates is formatted. The resources loaded are determined by the UI culture setting, and the formatting options are determined by the culture setting. The first place an application will look for culture values is the CurrentCulture and CurrentUICulture properties. They can be set in code as shown below.
The CurrentCulture property's default value is the system's User Locale, which is set in the Regional Options control panel. The CurrentUICulture property's default value is the system's UI Language, which is the language of your system UI. On Windows 2000 and Windows XP MultiLanguage Edition, the CurrentUICulture defaults to the current user UI language settings.
In some instances, you may want to have most of your application change according to the operating system's or user's culture settings, but also have a number or date that does not change. You can have culture-specific classes format the information with the invariant culture, which is associated with the English language but no particular region. For more information on these classes, see Formatting For Different Cultures and System.Globalization Namespace. For more information on the invariant culture, see CultureInfo.InvariantCulture Property. For a list of possible culture settings, see CultureInfo Class.
To set formatting options appropriate for a specific culture
- Set the CurrentCulture and CurrentUICulture properties if you wish to override the settings of the user or operating system. In general, you would want to specify a culture so that every part of the application's user interface is appropriate to that culture. So you must set it before the InitializeComponent method is called.
' Visual Basic ' Put the Imports statements at the beginning of the code module Imports System.Threading Imports System.Globalization ' Put the following code before InitializeComponent() ' Sets the culture to French (France) Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR") ' Sets the UI culture to French (France) Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR") // C# // Put the using statements at the beginning of the code module using System.Threading; using System.Globalization; // Put the following code before InitializeComponent() // Sets the culture to French (France) Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR"); // Sets the UI culture to French (France) Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");Note The culture value must always be a specific culture (such as "fr-FR"), not a neutral culture (such as "fr"). The reason is that a neutral culture such as "fr" can apply to all French-speaking cultures, and different currencies are used in France, Belgium, and Quebec.
- Call formatting methods with the invariant culture for any strings that should appear unchanged regardless of the CurrentCulture property's value.
' Visual Basic Dim MyInt As Integer = 100 Dim MyString As String = MyInt.ToString("C", CultureInfo.InvariantCulture) MessageBox.Show(MyString) // C# int MyInt = 100; string MyString = MyInt.ToString("C", CultureInfo.InvariantCulture); MessageBox.Show(MyString);