Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleLocalize control resources at run time by reloading the form in VB .NET
DescriptionThis example shows how to localize control resources at run time by reloading the form in VB .NET.
Keywordslocale, internationalization, globalization, CultureInfo, runtime
CategoriesVB.NET, Controls
 
First, build the form as you normally would. Then set the form's Localizable property to True and set its Language property to another language that you want to support. Change the controls' properties for the new language. For example, translate the text into the new language.

When you do this, Visual Basic stores information for the different languages in different resource files associated with the form. If you click the Solution Explorer's Show All Files button, you can see these files below the form.

Normally Visual Basic examines the computer's regional settings to decide which resources to use but you can change this for testing purposes at run time.

Subroutine SetLocale reloads a form's controls using a new locale. It creates a CultureInfo object for the locale and sets the thread's locale properties to it. The code then clears the form's Controls collection and calls its automatically-generated InitializeComponent method to load the controls. The current thread locale properties determine how the controls are loaded.

 
' Reload the form using this locale.
Private Sub SetLocale(ByVal locale_name As String)
    Static setting_locale As Boolean = False

    If setting_locale Then Exit Sub
    If Not Me.Created Then Exit Sub
    If Thread.CurrentThread.CurrentCulture.Name = _
        locale_name Then Exit Sub

    setting_locale = True

    ' Make a CultureInfo.
    Dim culture_info As New CultureInfo(locale_name)

    ' Make the thread use this locale.
    Thread.CurrentThread.CurrentUICulture = culture_info
    Thread.CurrentThread.CurrentCulture = culture_info

    ' Reload the form.
    If Me.components IsNot Nothing Then _
        Me.components.Dispose()
    Me.Controls.Clear()
    Me.InitializeComponent()

    setting_locale = False
End Sub
 
When you click the English or German radio buttons, their event handlers call SetLocale. They then check the newly selected radio button because this gets messed up when you reload the controls.
 
' Select English.
Private Sub radEnglish_CheckedChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    radEnglish.CheckedChanged
    If Not radEnglish.Checked Then Exit Sub
    SetLocale("en-US")
    radEnglish.Checked = True ' Reloading resets this.
End Sub

' Select German.
Private Sub radGerman_CheckedChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    radGerman.CheckedChanged
    If Not radGerman.Checked Then Exit Sub
    SetLocale("de-DE")
    radGerman.Checked = True ' Reloading resets this.
End Sub
 
The following code shows how the program displays a locale-aware message. This example stores strings in resource files names RuntimeStrings.resx and RuntimeStrings.de-DE.resx so Visual Basic makes a My.Resources.RuntimeStrings namespace for them. The program displays the string named Help_About. The resource manager automatically gets the correct version.
 
    ' Display Help About.
    Private Sub AboutToolStripMenuItem_Click(ByVal sender _
        As System.Object, ByVal e As System.EventArgs) _
        Handles mnuHelpAbout.Click
#If True Then
        ' The easy way
        MessageBox.Show(My.Resources.RuntimeStrings.Help_About)
#Else
        ' This vesion explicitly uses a ResourceManager.
        ' The base name is <ProjectName>.<FileName>.
        Dim resource_manager As New _
            ResourceManager("Localized.RuntimeStrings", _
            Me.GetType.Assembly)

        ' Display the message.
        MessageBox.Show(resource_manager.GetString("Help_About"))
#End If
    End Sub
 
Note that reloading the controls resets any data the user might have entered into them.

This example was written in Visual Basic 2005 but I think it should work in earlier vesions of Visual Basic .NET.

See also:

 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated