Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the language of a TextBox automatically [closed]

Tags:

c#

winforms

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?

like image 646
amirhossein Avatar asked Jun 21 '13 12:06

amirhossein


1 Answers

Things to check:

  1. Is "fa" an installed language?
  2. Have you attached textBox1_Enter and textBox1_Leave to events dispatched by textBox1?
  3. 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?
like image 176
David Arno Avatar answered Oct 20 '22 23:10

David Arno