如何在运行时更改WinForms应用程序的文化

Eni*_*ate 11 .net c# localization winforms

我用C#创建了Windows Form程序.本地化有一些问题.我有2种语言的资源文件(一种用于英语,另一种用于法语).我想单击每个语言按钮并在运行时更改语言.

但是,当我点击按钮时,它不起作用.我正在使用此代码.

private void btnfrench_Click(object sender, EventArgs e)
{
    getlanguage("fr-FR");
}

private void getlanguage(string lan)
{
    foreach (Control c in this.Controls)
    {
        ComponentResourceManager cmp = 
            new ComponentResourceManager(typeof(BanksForm));
        cmp.ApplyResources(c, c.Name, new CultureInfo(lan));
    }
}
Run Code Online (Sandbox Code Playgroud)

请问有什么帮助......

非常感谢....

Han*_*ant 25

这有效:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

小心避免添加这样的口哨,没有人会使用.它充其量只是一个演示功能,实际上用户不会即时更改其母语.


kev*_*v22 6

您可能必须在控件上递归调用ApplyResources:

private void btnfrench_Click(object sender, EventArgs e)
{
    ApplyResourceToControl(
        this, 
        new ComponentResourceManager(typeof(BanksForm)), 
        new CultureInfo("fr-FR"))
}

private void ApplyResourceToControl(
   Control control, 
   ComponentResourceManager cmp, 
   CultureInfo cultureInfo)
{
    cmp.ApplyResources(control, control.Name, cultureInfo);

    foreach (Control child in control.Controls)
    {
        ApplyResourceToControl(child, cmp, cultureInfo);
    }
}
Run Code Online (Sandbox Code Playgroud)