How to change the language of a TextBox automatically

I have a Winforms application in c# and I want a TextBox to change language automatically when it gets focused.

I tried this code:

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;
}

But when I enter the textBox, the language does not change. What can I do?


Things to check:

  • Is "fa" an installed language?
  • Have you attached textBox1_Enter and textBox1_Leave to events dispatched by textBox1?
  • Have you run it via the debugger and checked GetInputLanguageByName is called and that the correct language is called when focus is gained and lost?

  • 尝试这个 ..

    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))
        {
            InputLanguage.CurrentInputLanguage = Lng;
        }
    
    } 
    
    }
    
    链接地址: http://www.djcxy.com/p/72242.html

    上一篇: 我怎样才能加载HTML装置在卡莫与摩卡Javascript单元测试?

    下一篇: 如何自动更改文本框的语言