7

I want when the focus enters in a TextBox, change the language to an specific language (for example persian) and when the focus leaves TextBox, change the language to original language which was set before.

How to change input-language in a windows forms application when a specific control is focused?

Here is what I tried, but I don't want the user press any key, I want to change the language automatically.

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if(e.Shift && e.Alt)
    {
        MessageBox.Show("***language of keybord changed***");
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
mohammad sadeq
  • 81
  • 1
  • 1
  • 2
  • That is a hotkey for the language bar, you will not see it. If you want to know about it then use the [InputLanguageChanged event](https://msdn.microsoft.com/en-us/library/system.windows.forms.form.inputlanguagechanged%28v=vs.110%29.aspx) instead. – Hans Passant Mar 05 '16 at 13:56
  • You can change the input language programmatically using [`InputLanguage.CurrentInputLanguage`](https://msdn.microsoft.com/en-us/library/system.windows.forms.inputlanguage.currentinputlanguage(v=vs.110).aspx). It's enough to handle [`Enter`](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.enter(v=vs.110).aspx) and set it to desired language and handle [`Leave`](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.leave(v=vs.110).aspx) event and set it back to previous selected input language. – Reza Aghaei Mar 05 '16 at 15:59

3 Answers3

17

You can change the input language programmatically using InputLanguage.CurrentInputLanguage.

It's enough to handle Enter event of your control and set the InputLanguage.CurrentInputLanguage to desired language and also handle Leave event of the control and set it back to previous selected input language.

In the below code, I set the input language to Persian when I enter TextBox1 and set it to previous language when I leave the control:

InputLanguage original;
private void textBox1_Enter(object sender, EventArgs e)
{
    original = InputLanguage.CurrentInputLanguage;
    var culture = System.Globalization.CultureInfo.GetCultureInfo("fa-IR");
    var language = InputLanguage.FromCulture(culture);
    if (InputLanguage.InstalledInputLanguages.IndexOf(language) >= 0)
        InputLanguage.CurrentInputLanguage = language;
    else
        InputLanguage.CurrentInputLanguage = InputLanguage.DefaultInputLanguage;
}

private void textBox1_Leave(object sender, EventArgs e)
{
    InputLanguage.CurrentInputLanguage = original;
}

To test the example you should have fa-IR as input language installed on your OS, otherwise it will set the language to default input language. You can use another culture input-language which you know installed on your OS.

Note: If you extensively need such feature in your forms, as an idea you can create an Extender Provider component providing an InputLanguage property. This way you can set the property at design-time. That's the way that components like ToolTip or HelpProvider works.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
3

For people seeking similar solution for other languages that are available in many countries (e.g.: Arabic, there is "ar-SA","ar-EG" and many others) You can use this to get the correct language regardless of the country:

For WinForms: VB:

originalInputLang = InputLanguage.CurrentInputLanguage
Dim lang = InputLanguage.InstalledInputLanguages.OfType(Of InputLanguage).Where(Function(l) l.Culture.Name.StartsWith("ar")).FirstOrDefault()
        If lang IsNot Nothing Then InputLanguage.CurrentInputLanguage = lang

C#:

originalInputLang = InputLanguage.CurrentInputLanguage;
var lang = InputLanguage.InstalledInputLanguages.OfType<InputLanguage>().Where(l => l.Culture.Name.StartsWith("ar")).FirstOrDefault();
if (lang != null) {
    InputLanguage.CurrentInputLanguage = lang;
}

For WPF: VB:

Dim origianl As Globalization.CultureInfo //outside the event handler



// in the entered event handler:
origianl = InputLanguageManager.Current.CurrentInputLanguage
Dim newLang = InputLanguageManager.
    Current.
    AvailableInputLanguages.
    OfType(Of Globalization.CultureInfo).
    Where(Function(i) i.Name.StartsWith("ar")).
    FirstOrDefault()
If newLang IsNot Nothing Then InputLanguageManager.Current.CurrentInputLanguage = newLang

//in the leave event handler:
InputLanguageManager.Current.CurrentInputLanguage = original

C#:

System.Globalization.CultureInfo original; // outside the event handlers

// in the enter event handler:
original = InputLanguageManager.Current.CurrentInputLanguage;
var newLang = InputLanguageManager.
    Current.
    AvailableInputLanguages.
    OfType<System.Globalization.CultureInfo>().
    Where(l => l.Name.StartsWith("ar")).
    FirstOrDefault();

if (newLang != null)
{
    InputLanguageManager.Current.CurrentInputLanguage = newLang;
}

// in the leave event handler:
InputLanguageManager.Current.CurrentInputLanguage = original;
Ahmad Kelany
  • 376
  • 3
  • 8
0

If you want to change the system input language (so that every application will use the selected language) you can simply use SendKeys which simulates keyboard key presses:

SendKeys.Send("%+");

The string represents Alt + Shift which in Windows changes the input language by default.

Bamdad
  • 726
  • 1
  • 11
  • 28