Article ID: 914356 - View products that this article applies to.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
This article describes how to set the culture programmatically in a Microsoft Windows-based application by using the My.Application.Culture property in Microsoft Visual Basic 2005. This article contains a step-by-step example that describes how the number display format changes, how the date display format changes, and how the time display format changes depending on the culture that you set in the Windows-based application.
This article describes how to set and access the current culture programmatically in a Windows-based application by using the My.Application.Culture class and the My.Application.ChangeCulture method in Microsoft Visual Basic 2005. The code examples illustrate how to set the current culture. It also shows how the number display format, the date display format, and the time display format change when you set the culture. Additionally this article describes the steps to create a Windows-based application that sets and accesses the current culture programmatically by using Visual Basic 2005.
The System.Globalization namespaceClasses that are defined in the System.Globalization namespace define the culture-related information such as languages, date format patterns, currency, numbers, and calendars. You can use the CurrentCulture property to set the culture and to obtain the culture information about the thread on which the code is running. To obtain the culture information, access the current thread on which the code is running, and then retrieve the CurrentCulture property from the thread.
The My.Application.Culture propertyTo retrieve the culture that the current thread uses for string manipulation and for string formatting, use the My.Application.Culture property. The My.Application.Culture property returns the CultureInfo object that the current thread uses. This object is identical to the one that is returned by the CurrentCulture property that controls many of the string-related computations on that thread. The CurrentCulture property determines the default formats for dates, times, currency, numbers, sorting order of text, string comparisons, and casing for all computations on the current thread.
The My.Application.ChangeCulture propertyTo change the culture that the current thread uses for string manipulation and for string formatting, use the My.Application.ChangeCulture method. The My.Application.ChangeCulture method changes the current thread's CurrentCulture property. The CurrentCulture property differs from a language setting. This property can only be set to a specific culture or to the invariant culture because the CurrentCulture property setting contains only data that is related to the standard settings for a geographical region.
Note When you change the culture information, the user's setting will be overridden and the related currency symbol may also be change. However, changing the culture information will not automatically change the value of the currency.
The invariant cultureThe invariant culture is culture-insensitive. You can specify the invariant culture by name using an empty string (""). Alternatively, you can specify the invariant culture by its culture identifier 0x007F. The CultureInfo.InvariantCulture property retrieves an instance of the invariant culture. It is associated with the English language but not with any country or region. You can use the invariant culture in most methods in the System.Globalization namespace that require a culture.
Note Except for the invariant culture, culture data is dynamic. This is true even for the predefined cultures. For example, countries or regions may change their currencies, their spellings of words, or their preferred calendar. Culture definitions track these regional changes. Custom cultures are subject to change without notice. And any specific culture might be overridden by a custom replacement culture. Also, an individual user can override cultural preferences. We recommend that you code your application to always obtain culture data at run time instead of assuming that culture data can be known in advance.
Classes in the System.Globalization namespaceThe following classes are the System.Globalization namespace classes. The examples in this article primarily use these classes to handle the regional settings.
The CultureInfo classThis class provides information about a specific culture or about the language. The culture name that is defined in the CultureInfo class follows the Request for Comments (RFC) 1766 standard. The culture name is in the following format:
languagecode2 - country/regioncode2In this example, languagecode2 is a lowercase two-letter code that is derived from International Organization for Standardization (ISO) 639-1. And country/regioncode2 is an uppercase two-letter code that is derived from ISO 3166. When a two-letter language code is unavailable for a culture name, you can use the three-letter code that is derived from ISO 639-2. However, some culture names have suffixes that specify the script.
The DateTimeFormatInfo classThis class defines how the date value and the time value are formatted and are displayed. How these values appear is based on the language that you select or on the culture that you select.
The NumberFormatInfo classThis class defines how the numeric values are formatted and are displayed. How these values appear is based on the language that you select.
RequirementsThe following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
Obtain the culture-specific display formatsThe following code example uses the My.Application.Culture property to retrieve the current culture name and to display a date in the current date and time format.
The following code example uses instances of the NumberFormatInfo class and the DateTimeFormatInfo class to display culture-specific display formats. The NumberFormatInfo class obtains the correct culture-specific format from the My.Application.Culture.NumberFormat property. The DateTimeFormatInfo class obtains the correct culture-specific format from theMy.Application.Culture.DateTimeFormat property.
Change the culture of the current threadThe following code example uses the My.Application.ChangeCulture method to change the culture.
Use the My.Application.ChangeCulture method to set the cultureThe following steps describe how to use the GetCultures method of the CultureInfo class to obtain the cultures that are installed on your computer. Additionally, these steps show how to display the cultures that are installed on your computer in a ComboBox control.
To create a Visual Basic 2005 application that uses the My.Application.Culture property and the My.Application.ChangeCulture method, follow these steps:
Display the NumberFormat property and the DateTimeFormat propertyYou can use the DateTimeFormat property to return the date setting for the current culture and the time setting for the current culture to the DateTimeFormatInfo class instance. To do this, follow these steps.
Note You can use the NumberFormat property to return the number settings for the current culture to the NumberFormatInfo class instance.
Number format, Date format, and Time format sampleYou can display numbers, the date, and the time in the format of the culture that you selected by using ComboBox1 on the Form1 form. To do this, follow these steps:
Verify that the application works
TroubleshootingTo prevent data from becoming unreadable or from changing in meaning, you must use one of the following formats when you save the data:
For more information about globalization in the Microsoft .NET Framework, click the following article number to view the article in the Microsoft Knowledge Base:
893663For more information about the CultureInfo class, visit the following Microsoft Developer Network (MSDN) Web site:
(http://support.microsoft.com/kb/893663/ )Globalization issues in ASP and ASP.NET
http://msdn.microsoft.com/msdnmag/issues/05/03/CultureInfo/default.aspxFor more information about how to developing world-ready applications by using the .NET Framework, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/h6270d0z(vs.71).aspxFor more information about globalization and localization namespaces in Visual Studio, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/3xbc3357(vs.71).aspxFor more information about how culture affects strings in Visual Basic, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/ms235309.aspxFor more information about the System.Globalization namespace, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/abeh092z.aspxFor more information about the CultureInfo class, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/system.globalization.cultureinfo.aspxFor more information about the My.Application.Culture property, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/81b19cf5-713f-47b8-a022-c9aa4cfcb191.aspxFor more information about the My.Application.ChangeCulture method, visit the following MSDN Web site:
Contact us for more help
Connect with Answer Desk for expert help.