使用资源更改WinForm的语言 [英] Change language of WinForm with resource

查看:151
本文介绍了使用资源更改WinForm的语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下方法改变winform的语言。

I have the following method that changes the language of the winform.

    private void LoadLanguage(string lang)
    {
        foreach (Control c in this.Controls)
        {
            ComponentResourceManager resources = new ComponentResourceManager(typeof(MainForm));
            resources.ApplyResources(c, c.Name, new CultureInfo(lang));
        }
    }

我在 Form_Load 方法。在窗体中,我有一个选项卡控件,但tabPage文本属性不会更改。另一方面, Label 正确地更改为适当的语言。任何建议?

I call this method on the Form_Load method. Inside the form i have a tab control but the tabPage text property does not change. On the other hand the Label are changed correctly to the appropriate language. Any suggestions?

推荐答案

删除您的方法,尝试在Program.cs文件中这样做:

Remove your method and try to do like this in Program.cs file:

//Add this line
Thread.CurrentThread.CurrentUICulture = new CultureInfo(languageString);
Application.Run(new Form());

编辑:

你的代码不工作是你应用语言的表单控件。这意味着你应用到TabControl控件,但TabControl也有控件(标签页)里面。因此,您需要通过控件迭代地迭代,以便为所有控件和子控件应用语言。尝试此代码:

The main thing why your code not working is that you applying language for form controls. This means that you applying to TabControl control, but TabControl also have controls(tab pages) "inside". So you need to iterate recursively through controls to apply language for all controls and sub controls. Try this code:

private void LoadLanguage(string lang)
{
    ComponentResourceManager resources = new ComponentResourceManager(typeof(main));
    CultureInfo cultureInfo = new CultureInfo(lang);

    doRecursiveLoading(this, cultureInfo, resources);
}

private void doRecursiveLoading(Control parent, CultureInfo cultureInfo,  ComponentResourceManager resources)
{
    foreach (Control c in parent.Controls)
    {
        resources.ApplyResources(c, c.Name, cultureInfo);
        if (c.Controls.Count > 0)
            doRecursiveLoading(c, cultureInfo, resources);
     }
 }

这篇关于使用资源更改WinForm的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆