Microsoft KB Archive/821772

= How to set culture information programmatically in a Microsoft Windows application by using Visual Basic .NET or Visual Basic 2005 =

Article ID: 821772

Article Last Modified on 5/18/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



IN THIS TASK

 * SUMMARY
 * Requirements
 * Use the CultureInfo Class to Set the Culture
 * Display the NumberFormat Property and Display the DateTimeFormat Property
 * Number Format, Date Format, and Time Format Sample
 * Verify That It Works
 * REFERENCES



SUMMARY
This article describes how to set the culture programmatically in a Microsoft Windows application. This article contains a step-by-step example that describes how the number display format changes, the date display format changes, and the time display format changes with the culture that you set in the Windows application.

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. CurrentCulture is a property of the thread that the code is running on. To obtain the culture information, gain access to the current thread that the code is running on, and then retrieve the CurrentCulture property from the thread.

The following classes are the System.Globalization namespace classes. In the examples in this article, these classes are primarily used to handle the regional settings.
 * The CultureInfo 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 -  format (where   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 not available 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 defines how the date value and the time value are formatted and are displayed based on the language that you select or based on the culture that you select.
 * The NumberFormatInfo class defines how the numeric values are formatted and are displayed, based on the language that you select.

back to the top

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

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

back to the top

Use the CultureInfo Class 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, and then how to display the cultures that are installed on your computer in a ComboBox control.  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Under Project Types, click Visual Basic Projects.

Note In Visual Studio 2005, click Visual Basic under Project Types. Under Templates, click Windows Application, and then click OK.

By default, Form1 is created. Add a ComboBox control to Form1.

By default, ComboBox1 is created. Add a Label control to Form1.

Note Put Label1 above ComboBox1 or put Label1 on the left side of ComboBox1. Right-click Label1, and then click Properties.</li> In the Properties dialog box, set the Text property to Select a Regional Language.</li>  Add the following statements at the beginning of the code in Form1: Imports System.Globalization Imports System.Threading </li>  Add the following code to the Form1_Load event handler: ComboBox1.Text = &quot;&quot; 'Get the installed cultures on your system. 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 Thread.CurrentThread.CurrentCulture = ci           End If        Next </li></ol>

back to the top

Display the NumberFormat Property and Display the DateTimeFormat Property
The following steps describe how to use the DateTimeFormat property to return the date setting for the current culture and to return the time setting for the current culture to the DateTimeFormatInfo class instance. 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 Form1.</li> In the Properties dialog box of Button1, set the Text property to Regional Display Format.</li> On the Project menu, click Add Windows Form.</li> In the Add New Item - WindowsApplication1 dialog box, click Open.

By default, Form2 is created.</li> Add four TextBox controls to Form2.</li> Add four Label controls to Form2.

Note Put each Label control on the left side of the TextBox control.</li> In the Properties dialog box, set the Text property of the Label controls to the following values:

</li> Add the ListBox control to Form2, and then add the Label control to Form2.

Note Put the Label control on the left side of ListBox1.</li> Set the Text property of Label5 to Months.</li>  Add the following code to the Button1_Click event handler on Form1: 'Get the number format of the current culture that is being used. Dim ni As NumberFormatInfo = Thread.CurrentThread.CurrentCulture.NumberFormat 'Get the DateTimeFormat of the current culture. Dim dti As DateTimeFormatInfo = Thread.CurrentThread.CurrentCulture.DateTimeFormat 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></ol>

back to the top

Number Format, Date Format, and Time Format Sample
The following steps describe how to display the numbers, the date, and the time in the format of the culture that you selected by using the ComboBox control: <ol> Add a Button control to Form1.</li> In the Properties dialog box, set the Text property of Button2 to Show Example for Display Format.</li> <li>On the Project menu, click Add Windows Form.</li> <li>In the Add New Item - WindowsApplication1 dialog box, click Open.

By default, Form3 is created.</li> <li>Add three TextBox controls to Form3, and then add three Label controls to Form3.

Note Put each label control on the left side of the TextBox control.</li> <li>Set the Text property of Label1 to First Number, set the Text property of Label2 to Second Number, and then set the Text property of Label3 to Result.</li> <li>Add two Button controls to Form3.</li> <li>In the Properties dialog box, set the Text property of Button1 to Display Date and Time.</li> <li>Set the Text property of Button2 to Display Sum.</li> <li> Add the following code to the Button1_Click event handler on Form3: 'Display the current date and time. MessageBox.Show(Now) </li> <li> Add the following code to the Button2_Click event handler on Form3: 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 two numbers in the TextBox. TextBox3.Text = Format(res, &quot;standard&quot;) </li> <li> Add the following code to the Button2_Click event handler on Form1: 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></ol>

back to the top

Verify That It Works

 * 1) On the Build menu, click Build Solution.
 * 2) On the Debug menu, click Start.

By default, Form1 is displayed.
 * 1) On Form1, select a language from the Select Language list in the ComboBox control.
 * 2) Click Regional Display Format to see the information about the number format, the date format, and the time format in the language that you selected.
 * 3) Click Show Example for Display Format.
 * 4) Click Display Sum to add two numbers.
 * 5) Type the numbers in the two input boxes that are displayed, and then click OK.

The numbers are displayed in the first two text boxes. The result is displayed in the third text box in the number format of the language that you selected.
 * 1) Click Display Date and Time.

The date and time are displayed in the date format and in the time format of the language that you selected.

back to the top

<div class="references_section">