Microsoft KB Archive/914356

= How to set culture information programmatically in a Windows-based application by using Visual Basic 2005 =

Article ID: 914356

Article Last Modified on 11/22/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005

-





SUMMARY
''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.''



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.



INTRODUCTION
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 namespace
Classes 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 property
To 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 property
To 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 culture
The invariant culture is culture-insensitive. You can specify the invariant culture by name using an empty string (&quot;&quot;). 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 namespace
The following classes are the System.Globalization namespace classes. The examples in this article primarily use these classes to handle the regional settings.

The CultureInfo class
This 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:

-

In this example,  is a lowercase two-letter code that is derived from International Organization for Standardization (ISO) 639-1. And  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 class
This 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 class
This class defines how the numeric values are formatted and are displayed. How these values appear is based on the language that you select.

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft .NET Framework
 * Microsoft Visual Basic 2005 or Microsoft Visual Basic .NET

This article assumes that you are familiar with the following topics:
 * Programming in Visual Basic 2005 or in Visual Basic .NET
 * The IDE in Microsoft Visual Studio 2005 or in Microsoft Visual Studio .NET

Obtain the culture-specific display formats
The 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. ' Get the current culture. Dim currentculture As String = My.Application.Culture.Name Dim jan1 As New Date(2006, 1, 1, 15, 15, 15) MsgBox(&quot;Current culture is &quot; & currentculture) ' Display a date string in the current culture MsgBox(&quot;Date represented in &quot; + currentculture + &quot;: &quot; & jan1) 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. ' Get the number format of the current culture that is being used. Dim ni As System.Globalization.NumberFormatInfo = My.Application.Culture.NumberFormat ' Get the DateTimeFormat of the current culture. Dim dti As System.Globalization.DateTimeFormatInfo = My.Application.Culture.DateTimeFormat ' Display various culture display formats MsgBox(&quot;Number Decimal Separator is: [&quot; + ni.NumberDecimalSeparator + &quot;]&quot;) MsgBox(&quot;Number Group Separator is: [&quot; + ni.NumberGroupSeparator + &quot;]&quot;) MsgBox(&quot;Time Pattern is: [&quot; + dti.LongTimePattern + &quot;]&quot;)

Change the culture of the current thread
The following code example uses the My.Application.ChangeCulture method to change the culture. ' Store the current culture. Dim originalculture As String = My.Application.Culture.Name Dim currentculture As String

' Create a date Dim jan1 As New Date(2005, 1, 1, 15, 15, 15)

' Set the culture to Dutch - The Netherlands. My.Application.ChangeCulture(&quot;nl-NL&quot;) currentculture = My.Application.Culture.Name 'Display the date in the current (dutch) culture MsgBox(&quot;Date represented in &quot; + currentculture + &quot;: &quot; & jan1)

' Set the culture to the invariant culture My.Application.ChangeCulture(&quot;&quot;) ' Display the date in the invariant culture MsgBox(&quot;Date represented in invariant culture: &quot; & jan1)

' Restore the culture to the original My.Application.ChangeCulture(originalculture) currentculture = My.Application.Culture.Name ' Display the date in the current (original) culture MsgBox(&quot;Date represented in &quot; + currentculture + &quot;: &quot; & jan1)

Use the My.Application.ChangeCulture method to set the culture
The 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:  Start Visual Basic 2005 Express Edition. On the File menu, click New Project. In the New Project dialog box, click Windows Application. Type CultureInfoExample in the Name box, and then click OK. By default, a form that is named Form1 is created. On the File menu, click Save All, and then click OK. Add a ComboBox control to the Form1 form. By default, a ComboBox control that is named ComboBox1 is created. Add a Label control to the Form1 form. By default, a Label control that is named Label1 is created. Put Label1 above ComboBox1, or put Label1 to the left of ComboBox1.</li> Right-click Label1, and then click Properties.</li> Type Select a Regional Language in the Text box.</li> On the View menu, click Code.</li>  On the Form1 form, add the following statement at the start of the code. Imports System.Globalization </li>  Add the following code to the Form1_Load event handler. ComboBox1.Text = &quot;&quot; ' Get the installed cultures on your computer Dim ci As CultureInfo For Each ci In CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures) ' Display the cultures as the items in the ComboBox ComboBox1.Items.Add(ci.DisplayName) ' Sort the items in the ComboBox. ComboBox1.Sorted = True Next </li>  Add the following code to the ComboBox1_SelectedValueChanged event handler. Dim ci As CultureInfo For Each ci In CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures) If ci.DisplayName = ComboBox1.SelectedItem Then My.Application.ChangeCulture(ci.Name) End If Next </li> Press CTRL+SHIFT+S to save the project.</li></ol>

Display the NumberFormat property and the DateTimeFormat property
You 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. <ol> Add a Button control to the Form1 form. By default, a Button control that is named Button1 is created.</li> Right-click Button1, and then click Properties.</li> Type Regional Display Format in the Text box.</li> On the Project menu, click Add Windows Form.</li> In the Add New Item - CultureInfoExample dialog box, click Windows Form, and then click Add. By default, a form that is named Form2 is created.</li> Add four TextBox controls to the Form2 form. By default, four TextBox controls that are named TextBox1, TextBox2, TextBox3, and TextBox4 are created.</li> Add four Label controls to the Form2 form. By default, four Label controls that are named Label1, Label2, Label3, and Label4 are created. Put each Label control to the left of a corresponding TextBox control.</li> In the Properties dialog box, set the Text property of the Label controls to the following values. </li> <li>Add a ListBox control to the Form2 form. Then, add a Label control to the Form2 form. By default, a Label control that is name Label5 and a ListBox control that is named ListBox1 are created. Put Label5 to the left of ListBox1.</li> <li>Right-click Label5, and then type Months in the Text box.</li> <li>On the View menu, click Code.</li> <li> On the Form1 form, add the following code to the Button1_Click event handler. ' Get the number format of the current culture. Dim ni As NumberFormatInfo = My.Application.Culture.NumberFormat ' Get the DateTimeFormat of the current culture. Dim dti As DateTimeFormatInfo = My.Application.Culture.DateTimeFormat ' Create Form2 and populate the TextBox controls. Dim MyForm As New Form2 ' Display the formats in the controls on Form2. MyForm.TextBox1.Text = ni.NumberDecimalSeparator MyForm.TextBox2.Text = ni.NumberGroupSeparator MyForm.TextBox3.Text = dti.LongTimePattern MyForm.TextBox4.Text = ni.CurrencySymbol Dim s, st As String s = dti.MonthNames For Each st In s   MyForm.ListBox1.Items.Add(st) Next ' Display the form. MyForm.Show MyForm.Text = &quot; Number and Date Time Format in &quot; + ComboBox1.SelectedItem </li> <li>Press CTRL+SHIFT+S to save the project.</li></ol>

Number format, Date format, and Time format sample
You 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: <ol> <li>Add a Button control to the Form1 form. By default, a Button control that is named Button2 is created.</li> <li>Right-click Button2, and then type Show Example for Display Format in the Text box.</li> <li>On the Project menu, click Add Windows Form.</li> <li>In the Add New Item - CultureInfoExample dialog box, click Windows Form, and then click Add. By default, a form that is named Form3 is created.</li> <li>Add three TextBox controls to the Form3 form. By default, three TextBox controls that are named TextBox1, TextBox2, and TextBox3 are created.</li> <li>Add three Label controls to the Form3 form. By default, three Label controls that are named Label1, Label2, and Label3 are created. Put each Label control to the left of a corresponding TextBox control.</li> <li>In the Properties dialog box, set the Text property of the Label controls to the following values. </li> <li>Add two Button controls to the Form3 form. By default, two Button controls that are named Button1 and Button2 are created.</li> <li>Right-click Button1, and then type Display Sum in the Text box.</li> <li>Right-click Button2, and then type Display Date and Time in the Text box.</li> <li>On the View menu, click Code.</li> <li> On the Form3 form, add the following code to the Button1_Click event handler. Dim fnum, snum, res As Double ' Input the first number. fnum = InputBox(&quot;Enter the first number&quot;) ' Input the second number. snum = InputBox(&quot;Enter the second number&quot;) ' Display the numbers in the TextBox with the thousand separator. TextBox1.Text = Format(fnum, &quot;standard&quot;) TextBox2.Text = Format(snum, &quot;standard&quot;) res = fnum + snum ' Display the sum of the two numbers in the TextBox. TextBox3.Text = Format(res, &quot;standard&quot;) </li> <li> On the Form3 form, add the following code to the Button2_Click event handler. ' Display the current date and time. MessageBox.Show(Now) </li> <li> On the Form1 form, add the following code to the Button2_Click event handler. Dim ExampleForm As New Form3 ' Display the form with an example for display format in the regional languages. ExampleForm.Show ExampleForm.Text = &quot;Example to show the Number and DateTime display format in &quot; + ComboBox1.SelectedItem </li> <li>Press CTRL+SHIFT+S to save the project.</li></ol>

Verify that the application works

 * 1) On the Build menu, click Build CultureInfoExample.
 * 2) On the Debug menu, click Start Debugging. The Form1 window opens.
 * 3) in the Form1 window, click to select a culture in the Select Language combo box.
 * 4) Click Regional Display Format. The Number and Date Time Format in    dialog box appears. This dialog box contains information about the number format, the date format, and the time format in the culture you have selected. Review this information, and then close the dialog box.
 * 5) Click Show Example for Display Format.
 * 6) To add two numbers, click Display Sum.
 * 7) Type the first number in the text box, and then click OK.
 * 8) Type the second number in the text box, and then click OK.

Note You must use the decimal symbol of the current culture when you type the numbers. The numbers that you provide are displayed in the first two text boxes. The sum of the two numbers is displayed in the third text box. The numbers are displayed in the number format of the culture that you selected.
 * 1) Click Display Date and Time. The date and the time are displayed in the date format and in the time format of the language that you selected.

Troubleshooting
To prevent data from becoming unreadable or from changing in meaning, you must use one of the following formats when you save the data:
 * The invariant culture
 * A binary format
 * A specific culture-independent format

Data that is saved according to the current values that are associated with a particular culture may become unreadable, or may change in meaning, if that culture changes. This behavior does not occur when data is saved according to the current values that are associated with the invariant culture.

<div class="references_section">