Multiple languages on the same WinForm

Hello,

I must write an application that must support input fields for a "left-to-right" language (as English) and input fields for a "right-to-left" language but on the same form.

The keyboard is bilangual (QWERTY and a "right-to-left" language).

When the user enters a left-to-right language input field, the keyboard keys must be QWERTY. When the user enters the right-to-left language input field, the keybord keys must automatically switch to the second language.

Is it possible with C#.NET/WinForm/Textbox

Thanks in advance,

Stephane.



Answer this question

Multiple languages on the same WinForm

  • Kishan

  • Rob Biernat

    Latest news: it's working (but just with a prototype) with C#.NET/WinForm

    Here is the code (C#).
    I suppose (not yet tested), you must have the used languages installed on your computer, especially for special keyboard layout (as arabic).
    This system expects you know which languages are installed. You must theorically check first if the expected languages are installed during program start.
    The Form can be localizable or not.

    Program.cs (standard)

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;

    namespace
    TestArabe
    {
    static class Program
    {
    [STAThread]
    static void Main()
    {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new FormTest());
    }
    }
    }


    Form1.CS

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Globalization;
    using
    System.Threading;

    namespace TestArabe
    {
    public partial class FormTest : Form
    {
    public FormTest()
    {
    InitializeComponent();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
    // Display the current input language
    // If you have more than the two languages used here, you can see that
    // the Culture Name changes when you press ALT-LeftSHIFT (Windows feature to switch
    //
    the keyboard layout).
    LabelInputLanguage.Text =
    Application.CurrentInputLanguage.Culture.Name;
    }

    private void TextEnglish_Enter(object sender, EventArgs e)
    {
    // Automatically switch to the french language (was english in my question but tested with FR)

    Application.CurrentInputLanguage = InputLanguage.FromCulture(new CultureInfo("fr-FR"));
    }

    private void TextArabs_Enter(object sender, EventArgs e)
    {
    // Automatically switch to the arabic@Algeria language (must be installed separately)
    // You can try with any installed culture

    Application
    .CurrentInputLanguage = InputLanguage.FromCulture(new CultureInfo("ar-DZ"));
    }

    private void FormTest_Load(object sender, EventArgs e)
    {
    // Just define which field/culture must be read from left to right and right to left
    TextEnglish.RightToLeft =
    RightToLeft.No;
    TextArabs.RightToLeft =
    RightToLeft.Yes;
    }
    }
    }

    Form1.Designer.cs

    namespace
    TestArabe
    {
    partial class FormTest
    {
    private System.ComponentModel.IContainer components = null;

    protected override void Dispose(bool disposing)
    {
    if (disposing && (components != null))
    {
    components.Dispose();
    }
    base.Dispose(disposing);
    }

    #region
    Windows Form Designer generated code
    private void InitializeComponent()
    {
    this.components = new System.ComponentModel.Container();
    System.ComponentModel.
    ComponentResourceManager resources = new system.ComponentModel.ComponentResourceManager(typeof(FormTest));
    this.TextEnglish = new System.Windows.Forms.TextBox();
    this.TextArabs = new System.Windows.Forms.TextBox();
    this.timer1 = new System.Windows.Forms.Timer(this.components);
    this.LabelInputLanguage = new System.Windows.Forms.Label();
    this.SuspendLayout();
    //
    // TextEnglish
    //
    resources.ApplyResources(this.TextEnglish, "TextEnglish");
    this.TextEnglish.Name = "TextEnglish";
    this.TextEnglish.Enter += new System.EventHandler(this.TextEnglish_Enter);
    //
    // TextArabs
    //
    resources.ApplyResources(this.TextArabs, "TextArabs");
    this.TextArabs.Name = "TextArabs";
    this.TextArabs.Enter += new System.EventHandler(this.TextArabs_Enter);
    //
    // timer1
    //
    this.timer1.Enabled = true;
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    //
    // LabelInputLanguage
    //
    resources.ApplyResources(this.LabelInputLanguage, "LabelInputLanguage");
    this.LabelInputLanguage.Name = "LabelInputLanguage";
    //
    // FormTest
    //
    resources.ApplyResources(this, "$this");
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.Controls.Add(this.LabelInputLanguage);
    this.Controls.Add(this.TextArabs);
    this.Controls.Add(this.TextEnglish);
    this.Name = "FormTest";
    this.Load += new System.EventHandler(this.FormTest_Load);
    this.ResumeLayout(false);
    this.PerformLayout();
    }
    #endregion

    private System.Windows.Forms.TextBox TextEnglish;
    private System.Windows.Forms.TextBox TextArabs;
    private System.Windows.Forms.Timer timer1;
    private System.Windows.Forms.Label LabelInputLanguage;
    }
    }


  • bryhhh

    The character set is also different (English and Arabic).

    This character set must change when entering an english input field or an arabic one.

     


  • Chris Mann - MSFT

    Interesting scenario. If I understand correctly, you would like the TextBox controls whose RightToLeft property is set to "True" to also automatically switch to using the Arabic keyboard layout (and then to English for a value of "False").

    .NET Framework 2.0 does include a new property called CultureInfo.KeyboardLayoutId. Unfortunately, it's read-only, so while you can get the current value, you can't set it. One possible work-around though would be to use this in conjunction with VB's My.Computer.Keyboard.SendKeys method. Based on the current KeyboardLayoutId value, you could send Alt+Shift via SendKeys to change to the next keyboard (until it reaches the value you need). This is a bit fragile though, and could conceivably lead to an infinite loop if the target value isn't found.

    I'm afraid that the only safe way I can think of to automate this scenario would be to pinvoke to the system keyboard APIs (e.g., LoadKeyboardLayout, ActivateKeyboardLayout).

    (If there *is* a way to do this purely in C#, I'd love to see it as well...)


  • msorens

    You can set the RightToLeft properties for the controls that are meant to support RightToLeft language's.


  • moogle2002

    Why do you want to do that, it sounds very evil

    Can you explane it a little bit more, meybe there is a other solution.


  • Eli Cohen

    wow

    very neat many thx for sharing this

    best regards



  • pmania

    Hi PJ and thanks for you interest.

    In facts this is an application writen in C#.NET/Winform used by Arabics people but using English as main language in there company (the the GUI is in English).

    This application will allow an user to insert textual information in a bilingual database. Each information can be encoded and stored in english and arabic. Thus, two input fields must be used (one in english with the QZERTY keyboard-layout and one in arabic with an automatic switch to the arabics characters of the same keyboard [it's a keyboard with two layouts]).

    It's also possible to ask to the user to switch the keyboard layout by using the Windows languages-bar but there is a risk to insert arabic information in english fields and english information in arabic fields (-> risks of database content inconsistency).

    Is it more clear

    Stephane.


  • Dan L

    hi,

    its something like data entry form for a 2 languages dictionary, you need every textbox stuck to particular language,

    i'm interested to know the answer of this question too

    best regards



  • Multiple languages on the same WinForm