What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleLocalize control resources at run time by reopening the form in VB .NET
DescriptionThis example shows how to localize control resources at run time by reopening 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.

Function LocalizedForm creates a CultureInfo object representing a locale and sets the thread's localization properties to it. It then creates a new instance of Form1. Visual Basic automatically uses the appropriate locale to build the form. The code positions the new form where the old form is and returns the new form.

' Reload the form using this locale.
Private Function LocalizedForm(ByVal locale_name As String, _
    ByVal checked_button As RadioButton, ByVal _
    unchecked_button As RadioButton) As Form1
    If Not Me.Created Then Return Nothing
    If Not checked_button.Checked Then Return Nothing

    ' 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

    Dim frm As New Form1
    frm.Location = Me.Location
    Return frm
End Function
When you click the English or German radio buttons, the program uses the following code to display the new form. It calls LocalizedForm to get a localized version. It checks the appropriate radio button on the new form, displays the form, and closes the old form.
' Select English.
Private Sub radEnglish_CheckedChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    Dim frm As Form1 = LocalizedForm("en-US", radEnglish, _
    If frm Is Nothing Then Exit Sub
    frm.radEnglish.Checked = True

End Sub

' Select German.
Private Sub radGerman_CheckedChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    Dim frm As Form1 = LocalizedForm("de-DE", radGerman, _
    If frm Is Nothing Then Exit Sub
    frm.radGerman.Checked = True

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
        ' This vesion explicitly uses a ResourceManager.
        ' The base name is <ProjectName>.<FileName>.
        Dim resource_manager As New _
            ResourceManager("Localized.RuntimeStrings", _

        ' Display the message.
#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.