Introduction
My attempt here is to give a tip to one of the most asked questions by beginners, "How to make language XXXXX as the input type to a Windows Form control?"
Background
I was developing a Windows Form application where the user input is in my mother tongue "Malayalam", but the issue is the user is not aware of Malayalam keyboard, so he wants a Google Transliteration type input... means the user will type the Malayalam words in English but the system will convert it to the Malayalam format. So I cannot simply set a font to the textbox
. I downloaded and installed the Microsoft indic tool for malyalam.
Using the Code
Step 1: First create a Windows Form application with a textbox
control in it.
Step 2: The Inputlanguage
of the textbox
must be Malayalam when the user starts typing and must change back to English when the user input is over. So create two events for Enter
and Leave
.
private void textBox2_Enter(object sender, EventArgs e)
{
}
private void textBox2_Leave(object sender, EventArgs e)
{
}
Step 3: Then, create a function to get the installed language:
public static InputLanguage GetInputLanguageByName(string inputName)
{
foreach (InputLanguage lang in InputLanguage.InstalledInputLanguages)
{
if (lang.Culture.EnglishName.ToLower().StartsWith(inputName))
return lang;
}
return null;
}
This function will get all the Inputlanguage
which has the Name
starting with the parameters given to the function.
Step 4: Create a function to set the keyboadlayout
to the input language selected.
public void SetKeyboardLayout(InputLanguage layout)
{
InputLanguage.CurrentInputLanguage = layout;
}
Step 5: Finally, call the function inside the Enter
and Leave
event of the Textbox
.
private void textBox2_Enter(object sender, EventArgs e)
{
SetKeyboardLayout(GetInputLanguageByName("mal"));
}
private void textBox2_Leave(object sender, EventArgs e)
{
SetKeyboardLayout(GetInputLanguageByName("eng"));
}
Points of Interest
Here, I had used only one textbox
control, we can use this in any Winform controls and also many other keyboards are available for free download over the web. So keeping this as a class and passing controls and the inputlanguagename
as parameter will help in reducing code irrespective of control and language.