Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change input-language in a windows forms application for a specific control?

Tags:

c#

.net

winforms

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***");
    }
}
like image 980
mohammad sadeq Avatar asked Mar 05 '16 11:03

mohammad sadeq


2 Answers

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.

like image 55
Reza Aghaei Avatar answered Sep 26 '22 10:09

Reza Aghaei


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;
like image 31
Ahmad Kelany Avatar answered Sep 22 '22 10:09

Ahmad Kelany