Click here to Skip to main content
15,894,907 members
Articles / Desktop Programming / Windows Forms

Windows Mobile Globalization in Visual Studio

Rate me:
Please Sign up or sign in to vote.
1.13/5 (7 votes)
19 Apr 2008Ms-PL1 min read 31.4K   204   14   9
Simple technique to support multiple languages in Compact Framework WinForms.

Introduction

The Compact Framework does not support changing Thread.CurrentUICulture to change a form's language on the fly.

Background

This code defines a custom version of System.Globalization.ComponentResourceManager in the global namespace that inherits from the standard version and overrides ApplyResource() to use a custom culture. Code generated in InitializeComponent() will compile using this version, and the Forms Editor will not break because you do not change the generated code. Wrap the function in "pragma warning disable 436" to suppress the warning about using an ambiguous class reference.

Use the References node in the Properties sheet to change the alias for System from "global" to "global,ms".

C#
extern alias ms;
using cm = ms::System.ComponentModel;
using System.Globalization;
using Wm2005Global;

namespace System.ComponentModel
{
    class ComponentResourceManager : cm.ComponentResourceManager
    {
        public ComponentResourceManager(Type type) : base(type) { }
        public override void ApplyResources(object value, 
                        string objectName, CultureInfo culture)
        {
            if (culture == null)
            {
                culture = Program.Culture;
            }
            base.ApplyResources(value, objectName, culture);
        }
    }
}

This shows how Visual Studio generates the normal code and uses the special version of ComponentResourceManager by default. The #pragma disables the warnings about using the ambiguous class:

C#
#region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    #pragma warning disable 436
    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = 
          new System.ComponentModel.ComponentResourceManager(typeof(Form1));
        this.mainMenu1 = new System.Windows.Forms.MainMenu();
        this.button1 = new System.Windows.Forms.Button();
        this.button2 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // button1
        // 
        resources.ApplyResources(this.button1, "button1");
        this.button1.Name = "button1";
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // button2
        // 
        resources.ApplyResources(this.button2, "button2");
        this.button2.Name = "button2";
        this.button2.Click += new System.EventHandler(this.button2_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
        resources.ApplyResources(this, "$this");
        this.Controls.Add(this.button2);
        this.Controls.Add(this.button1);
        this.Menu = this.mainMenu1;
        this.Name = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.ResumeLayout(false);

    }
#pragma warning restore 436
    #endregion

    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
}

I think this is the cleanest possible solution to Globalization on Windows Mobile 2005. You do not have to change the default language for the entire device, and you can even change the language on a form from a menu item without closing it! This also demonstrates a technique for disposing all the controls on a form, calling InitializeComponent() and Form_Load() again, so it's like your form is recreated without closing it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNeed common resource files instead of each form Pin
bagu4it14-Jun-13 1:38
bagu4it14-Jun-13 1:38 
QuestionHad somebody compiled those code passed? Pin
jeam25-Oct-11 3:33
jeam25-Oct-11 3:33 
GeneralBest Localization Plug-in for Visual Studio. Pin
Alexander Nesterenko17-Dec-08 21:44
Alexander Nesterenko17-Dec-08 21:44 
GeneralLook good Pin
Yves17-Nov-08 10:00
Yves17-Nov-08 10:00 
GeneralAnother hack to get around CurrentUICulture Pin
FkYkko6-Nov-08 3:22
FkYkko6-Nov-08 3:22 
GeneralBrillant! [modified] Pin
Pablo Grisafi4-May-08 10:53
Pablo Grisafi4-May-08 10:53 
GeneralMore explanation request Pin
Maruf Maniruzzaman19-Apr-08 16:22
Maruf Maniruzzaman19-Apr-08 16:22 
GeneralRe: More explanation request Pin
tbpub20-Apr-08 7:12
tbpub20-Apr-08 7:12 
GeneralRe: More explanation request Pin
Anand.E2-Apr-14 5:28
Anand.E2-Apr-14 5:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.