How do I change the culture of a WinForms application at runtime

后端 未结 3 678
攒了一身酷
攒了一身酷 2020-11-29 10:19

I have created Windows Form Program in C#. I have some problems with localization. I have resource files in 2 languages(one is for english and another is for french). I want

3条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 10:52

    This worked:

    private void button1_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-BE");
        ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
        resources.ApplyResources(this, "$this");
        applyResources(resources, this.Controls);
    }
    
    private void applyResources(ComponentResourceManager resources, Control.ControlCollection ctls)
    {
        foreach (Control ctl in ctls)
        {
            resources.ApplyResources(ctl, ctl.Name);
            applyResources(resources, ctl.Controls);
        }
    }
    

    Be careful to avoid adding whistles like this that nobody will ever use. It at best is a demo feature, in practice users don't change their native language on-the-fly.

提交回复
热议问题