简体   繁体   中英

Using another language in a textbox in C# winform

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.

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"));
        }
    }
}

.NET Apps are unicode. Go to your control panel, Regional and Language Options, and change your default input language to Malay. Your app should follow. (This presumes the storage of the input data is Unicode as well).

If you want to do this on the fly:

 System.Globalization.CultureInfo maylay = new System.Globalization.CultureInfo("ms");
 System.Threading.Thread.CurrentThread.CurrentCulture = malay;

For typing in Malayalam, there are some ready made keyboards available as free. I used the keyboard 'Malayalam Indic Input 2'. It worked perfect for me. Also heard that another keyboard 'Varamozhi' has a lot of features. Some other points are, when saving to database, you need save this malayalam as unicode. SQL query for the same is as follows:

    "Insert into table_1 values (N'"+textBox1.Text+"')";

The answer to my question lies below:

    private void textBox1_Enter(object sender, EventArgs e)
    {
        SetKeyboardLayout(GetInputLanguageByName("mal"));
    }

    private void textBox1_Leave(object sender, EventArgs e)
    {
        SetKeyboardLayout(GetInputLanguageByName("eng"));
    }
    public static InputLanguage GetInputLanguageByName(string inputName)
    {
        foreach (InputLanguage lang in InputLanguage.InstalledInputLanguages)
        {
            if (lang.Culture.EnglishName.ToLower().StartsWith(inputName))
                return lang;
        }
        return null;
    }
    public void SetKeyboardLayout(InputLanguage layout)
    {
        InputLanguage.CurrentInputLanguage = layout;
    }

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"));

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM