MORE INFORMATION
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 (""). 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 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/regioncode2
In 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.
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("Current culture is " & currentculture)
' Display a date string in the current culture
MsgBox("Date represented in " + currentculture + ": " & 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 the
My.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("Number Decimal Separator is: [" + ni.NumberDecimalSeparator + "]")
MsgBox("Number Group Separator is: [" + ni.NumberGroupSeparator + "]")
MsgBox("Time Pattern is: [" + dti.LongTimePattern + "]")
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("nl-NL")
currentculture = My.Application.Culture.Name
'Display the date in the current (dutch) culture
MsgBox("Date represented in " + currentculture + ": " & jan1)
' Set the culture to the invariant culture
My.Application.ChangeCulture("")
' Display the date in the invariant culture
MsgBox("Date represented in invariant culture: " & 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("Date represented in " + currentculture + ": " & 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.
- Right-click Label1, and then click
Properties.
- Type Select a Regional Language in
the Text box.
- On the View menu, click
Code.
- On
the Form1 form, add the following statement at the start of the code.
Imports System.Globalization
- Add the following code to the Form1_Load
event handler.
ComboBox1.Text = ""
' 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
- 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
- Press CTRL+SHIFT+S to save the project.
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.
- Add a Button control to the Form1 form. By default, a Button control that is named Button1 is
created.
- Right-click Button1, and then click
Properties.
- Type Regional Display Format in the
Text box.
- On the Project menu, click Add
Windows Form.
- 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.
- Add four TextBox controls to the Form2 form. By default, four TextBox controls that are named TextBox1, TextBox2,
TextBox3, and TextBox4 are created.
- 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.
- In the Properties dialog box, set the
Text property of the Label controls to the
following values.
Label control name | Text |
Label1 | Decimal Symbol |
Label2 | Digit Grouping Symbol |
Label3 | Percent Symbol |
Label4 | Currency Symbol |
- 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.
- Right-click Label5, and then type
Months in the Text box.
- On the View menu, click
Code.
- 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 = " Number and Date Time Format in " + ComboBox1.SelectedItem
- Press CTRL+SHIFT+S to save the project.
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:
- Add a Button control to the Form1 form. By default, a Button control that is named Button2 is
created.
- Right-click Button2, and then type Show Example
for Display Format in the Text box.
- On the Project menu, click Add
Windows Form.
- 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.
- Add three TextBox controls to the Form3 form. By default, three TextBox controls that are named TextBox1, TextBox2,
and TextBox3 are created.
- 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.
- In the Properties dialog box, set the
Text property of the Label controls to the
following values.
Label control name | Text |
Label1 | First Number |
Label2 | Second Number |
Label3 | Result |
- Add two Button controls to the Form3 form. By default, two Button controls that are named Button1 and Button2 are
created.
- Right-click Button1, and then type
Display Sum in the Text box.
- Right-click Button2, and then type
Display Date and Time in the Text
box.
- On the View menu, click
Code.
- 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("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 the two numbers in the TextBox.
TextBox3.Text = Format(res, "standard")
- On the Form3 form, add the following code to the
Button2_Click event handler.
' Display the current date and time.
MessageBox.Show(Now)
- 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 = "Example to show the Number and DateTime display format in " + ComboBox1.SelectedItem
- Press CTRL+SHIFT+S to save the project.
Verify that the application works
- On the Build menu, click Build
CultureInfoExample.
- On the Debug menu, click Start
Debugging. The Form1 window opens.
- in the Form1 window, click to select a culture in the
Select Language combo box.
- Click Regional Display Format. The Number and Date Time Format in SelectedLanguage (Locale) 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.
- Click Show Example for Display
Format.
- To add two numbers, click Display
Sum.
- Type the first number in the text box,
and then click OK.
- 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. - 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.
REFERENCES
For more information about globalization in the Microsoft .NET Framework, click the following article number to view the article in the Microsoft Knowledge Base:
893663
Globalization issues in ASP and
ASP.NET
For more information about globalization and localization, visit
the following Microsoft Web site:
For more information about the
CultureInfo class, visit the following Microsoft Developer Network (MSDN) Web site:
For more information about how to developing world-ready applications by using the .NET Framework, visit the following MSDN Web site:
For more information about globalization and localization namespaces in Visual Studio, visit the following MSDN Web site:
For more information about how culture affects strings in Visual Basic, visit the following MSDN Web site:
For more information about the
System.Globalization namespace, visit the following MSDN Web site:
For more information about the
CultureInfo class, visit the following MSDN Web site:
For more information about the
My.Application.Culture property, visit the following MSDN Web site:
For more information about the
My.Application.ChangeCulture method, visit the following MSDN Web site: