【问题标题】:How can i change my winform to another language如何将我的 winform 更改为另一种语言
【发布时间】:2019-08-17 05:21:13
【问题描述】:

我有一个 winform,我将表单的语言更改为荷兰语。之后,我编辑组框标签的名称等。当我更改单选按钮时,它只会更改组框的名称,而不是文本框的标签。

我将表单的语言更改为荷兰语。然后我制作了 2 个 Radiobuttons 荷兰语和德语。当我按下荷兰语单选按钮时,它只会更改组框的名称,而其他标签和内容保持不变。

这是我使用的方法和代码。

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

 private void DutchRadiobutton_CheckedChanged(object sender, EventArgs e)
        {
            ChangeLanguage("nl-NL");

        }

我希望在单击单选按钮荷兰语时更改整个表单以及其中的每个控件。目前只有组框名称发生变化,但其中的标签和文本框保持相同的语言。

【问题讨论】:

  • 您需要递归循环遍历控件,否则您不会将更改应用到 GroupBox(es) 的内部控件。
  • @Odrai 我怎么做那个天使?
  • 请看C# Recursion

标签: c# winforms radio-button


【解决方案1】:

递归示例

这个方法的输入可以是'this',例如您的控件或 GroupBox 的引用。

private void ApplyChanges(Control ctrl)
{
    foreach (Control c in ctrl.Controls)
    {
        // Do something

        Debug.WriteLine($"ctrl name: {c.Name}"); // Test code, just to print the control name(s).
        if (c.Controls != null && c.Controls.Count > 0)
        {
            ApplyChanges(ctrl);
        }
    }
}

语言转换方法

private void ApplyResources(Control parent, CultureInfo culture)
{
    _componentResourceManager.ApplyResources(parent, parent.Name, culture);

    foreach (Control ctl in parent.Controls)
    {
        ApplyResources(ctl, culture);
    }
}

或查看现有问题/答案:'Proper way to change language at runtime'

【讨论】:

  • Moet dit in de andere methode of moet ik dit apparat toevoegen ben nieuw in het programming
  • @Merve 请用英文写出您的 cmets,以便所有 StackOverflow 用户都能关注讨论。不过,我已经更新了我的帖子。
猜你喜欢
  • 2014-02-22
  • 1970-01-01
  • 1970-01-01
  • 2018-02-15
  • 1970-01-01
  • 2014-11-04
  • 2011-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多