How to change the textbox language automatically

I have a Winforms application in C # and I want the TextBox to automatically change the language when it focuses.

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 text box, the language does not change. What can I do?

+7
source share
2 answers

What you need to check:

  • Is "fa" an established language?
  • Did you attach textBox1_Enter and textBox1_Leave to events dispatched by textBox1?
  • Did you run it through the debugger and GetInputLanguageByName is called and that the correct language is called when the focus is received and lost?
+4
source

Try it.

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

All Articles