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
languagecode2-country/regioncode2
format (where 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 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 topRequirements
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 topUse 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.
- In the Properties dialog box, set the
Text property to Select a Regional
Language.
- Add the following statements at the beginning of the code in
Form1:
Imports System.Globalization
Imports System.Threading
- Add the following code to the Form1_Load
event handler:
ComboBox1.Text = ""
'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
- 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
back to the topDisplay 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.
- Add a Button control to Form1.
- In the Properties dialog box of
Button1, set the Text property to
Regional Display Format.
- On the Project menu, click Add
Windows Form.
- In the Add New Item - WindowsApplication1
dialog box, click Open.
By default, Form2 is
created. - Add four TextBox controls to
Form2.
- Add four Label controls to Form2.
Note Put each Label control
on the left side of the
TextBox control. - In the Properties dialog box, set the
Text property of the Label controls
to the following values:
Label Name | Text |
Label1 | Decimal
Symbol |
Label2 | Digit Grouping
Symbol |
Label3 | Percent
Symbol |
Label4 | Currency
Symbol |
- 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. - Set the Text property of
Label5 to Months.
- 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 = " Number and Date Time Format in " + ComboBox1.SelectedItem
back to the topNumber 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:
- Add a Button control to Form1.
- In the Properties dialog box, set the
Text property of Button2 to Show
Example for Display Format.
- On the Project menu, click Add
Windows Form.
- In the Add New Item - WindowsApplication1
dialog box, click Open.
By default, Form3 is
created. - 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. - 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.
- Add two Button controls to
Form3.
- In the Properties dialog box, set the
Text property of Button1 to Display
Date and Time.
- Set the Text property of
Button2 to Display Sum.
- Add the following code to the
Button1_Click event handler on Form3:
'Display the current date and time.
MessageBox.Show(Now)
- Add the following code to the
Button2_Click event handler on Form3:
Dim fnum, snum, res As Double
'Input the first number.
fnum = InputBox("Enter the first Number")
'Input the second number.
snum = InputBox("Enter the second number")
'Display the numbers in the TextBox with the thousand separator.
TextBox1.Text = Format(fnum, "standard")
TextBox2.Text = Format(snum, "standard")
res = fnum + snum
'Display the sum of two numbers in the TextBox.
TextBox3.Text = Format(res, "standard")
- 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 = "Example to show the Number and DateTime display format in " + ComboBox1.SelectedItem
back to the top Verify That It Works
- On the Build menu, click Build
Solution.
- On the Debug menu, click
Start.
By default, Form1 is displayed. - On Form1, select a language from the Select Language list in the ComboBox control.
- 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.
- Click Show Example for Display
Format.
- Click Display Sum to add two
numbers.
- 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. - 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