C# 使用资源更改WinForm的语言
我使用以下方法更改winform的语言C# 使用资源更改WinForm的语言,c#,c#-4.0,C#,C# 4.0,我使用以下方法更改winform的语言 private void LoadLanguage(string lang) { foreach (Control c in this.Controls) { ComponentResourceManager resources = new ComponentResourceManager(typeof(MainForm)); resources.ApplyRe
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\u Load
方法中调用此方法。在表单中,我有一个tab控件,但是tabPage文本属性没有改变。另一方面,标签
被正确地更改为适当的语言。有什么建议吗?删除您的方法,并尝试在Program.cs文件中执行以下操作:
//Add this line
Thread.CurrentThread.CurrentUICulture = new CultureInfo(languageString);
Application.Run(new Form());
编辑:
代码不起作用的主要原因是应用了表单控件的语言。这意味着您可以应用于TabControl控件,但TabControl也有“内部”控件(选项卡页)。所以您需要递归地遍历控件,以便为所有控件和子控件应用语言。请尝试以下代码:
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);
}
}
您是否在resx文件中翻译了tabpage文本?是的,我在资源中有相应的文本。您是否尝试使用测试应用程序来测试tabpage的本地化是否正确?是的,但我要求用户在第一个表单上选择语言。你知道我怎样才能在program.cs中工作吗?我想更改我表格的标题。您必须添加这一行:this.Text=resources.GetString(“MainTitle”,cultureInfo);在LoadLanguage方法中调用doRecursiveLoading并在每种语言的资源文件中添加一个字段“MainTitle”之后,必须添加此行。