Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using another language in a textbox in C# winform

Tags:

c#

winforms

I know that this is a much discussed topic here and in other blogs but none of the techniques could help me out.

I want to type in Malayalam Language in a text box. I did this so far. I have installed a font 'AnjaliOldLipi'. I can type in Malayalam in Notepad. But I can't do the same in Winform application. It appears as English in textbox.

I tried the following code with no result.

private void richTextBox_test_Leave(object sender, EventArgs e)
{
    System.Globalization.CultureInfo TypeOfLanguage = new 
    System.Globalization.CultureInfo("en-us");
    InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(TypeOfLanguage);
}

private void richTextBox_test_Enter(object sender, EventArgs e)
{
    MessageBox.Show("textbox ebntereed");
    System.Globalization.CultureInfo TypeOfLanguage = new System.Globalization.CultureInfo("ms-MY");
    InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(TypeOfLanguage);
    richTextBox_test.Font = new Font("AnjaliOldLipi", 12);
}

Then I tried the following code. Now the default keyboard is changing(I can see it on my task bar) when I enter the text box. Still when typing, the text is appearing in English. I need to press 'Ctrl+Shift' to write in 'malayalam'. I don't know why but I need to write in 'malayalam' without pressing any keyboard buttons.

like image 256
Foreever Avatar asked Aug 04 '13 14:08

Foreever


2 Answers

Simplicity is everything in successful program, no need for complexity, save your effort for more demanding things, and try this:

using System;
using System.Windows.Forms;
using System.Globalization;

namespace TestingTextBoxAutoLangSwitch
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // Switching to Arabic Jordan
        private void textBox2_Enter_1(object sender, EventArgs e)
        {
            Application.CurrentInputLanguage = InputLanguage.FromCulture(new CultureInfo("ar-jo"));
        }

        // Switching back to English USA
        private void textBox2_Leave(object sender, EventArgs e)
        {
            Application.CurrentInputLanguage = InputLanguage.FromCulture(new CultureInfo("en-us"));
        }
    }
}
like image 164
Ashraf Sada Avatar answered Sep 23 '22 08:09

Ashraf Sada


I use these codes: First of all you must find the name of the culture language you want. method "GetInutLanguageByName" will return the language that you requested Then you will check whether you installed the requested language or not, if yes then return the requested language. Then change the input language very easy...

private static InputLanguage GetInutLanguageByName(string layOut)
    {
        foreach (InputLanguage lng in InputLanguage.InstalledInputLanguages)
        {
            if (lng.Culture.DisplayName == layOut)
            {
                return lng;
            }
        }
        return null;

    }

private void SetKeyboardLayout(InputLanguage Layout)
    {
        InputLanguage.CurrentInputLanguage = Layout;
    }

private void FirstNameTextBox_Enter(object sender, EventArgs e)
    {

        SetKeyboardLayout(GetInutLanguageByName("Persian"));

    }
like image 35
Alireza Ali Avatar answered Sep 22 '22 08:09

Alireza Ali