我在c#中有一个Winforms应用程序,我希望TextBox在聚焦时自动更改语言。
我试过这段代码:
private void textBox1_Enter(object sender, EventArgs e)
{
SetKeyboardLayout(GetInputLanguageByName("fa"));
}
private void textBox1_Leave(object sender, EventArgs e)
{
SetKeyboardLayout(GetInputLanguageByName("eng"));
}
public static InputLanguage GetInputLanguageByName(string inputName)
{
foreach (InputLanguage lang in InputLanguage.InstalledInputLanguages)
{
if (lang.Culture.EnglishName.ToLower().StartsWith(inputName))
{
return lang;
}
}
return null;
}
private void SetKeyboardLayout(InputLanguage layout)
{
InputLanguage.CurrentInputLanguage = layout;
}
但是当我输入textBox时,语言不会改变。我该怎么办?
答案 0 :(得分:5)
要检查的事项:
答案 1 :(得分:1)
试试这个..
private void textBox1_Enter(object sender, EventArgs e)
{
SetKeyboardLayout("FA");
}
private void SetKeyboardLayout(InputLanguage layout)
{
foreach (InputLanguage Lng in InputLanguage.InstalledInputLanguages)
{
if (Lng.Culture.EnglishName.ToUpper().StartsWith(layout.ToString()))
{
InputLanguage.CurrentInputLanguage = Lng;
}
}
}