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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…