Changing the input language in a textbox at runtime


Today, I’ll talk about changing the input language for a textbox at runtime, in VS 2005. The might have two textboxes and each would expect a different input language. In my example, I need TextBox1 to receive Arabic input, while TextBox2 should receive English input.


These are the steps to do so:
Step 1: Enumerate the InputLanguage available on your machine, get the keyboard for Arabic and English. You don’t really know the available keyboards that are installed on the user machine, so you better validate that the keyboard exists
Step 2: Handle the TexBox.Enter event.
Step 3: Load the relevant Keyboard.
Step 4: Reload the default Keyboard, when you no longer need the Arabic Keyboard. To simplify my solution I assumed that I’ll change the keyboard to English only on TextBox2. On the other hand, you may change the inputlanguage on TextBox1.Leave.


This is the actual code (in VB, to please VB developers :))



Private ArabicInput As InputLanguage
Private EnglishInput As InputLanguage
Private
Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  ‘ Set the default as the current Inputlanguage
  ArabicInput = InputLanguage.CurrentInputLanguage
  EnglishInput = InputLanguage.CurrentInputLanguage
  ‘Iterate to find the available Arabic and English Keyboards
  Dim count As Integer
  count = InputLanguage.InstalledInputLanguages.Count
  For i As Integer = 1 To (count – 1)
   If InputLanguage.InstalledInputLanguages(i).LayoutName.Contains(“Arabic”) = True Then
    ‘Found an Arabic Keyboard 
    ArabicInput = InputLanguage.InstalledInputLanguages(i)
   Else
    If InputLanguage.InstalledInputLanguages(i).LayoutName.Contains(“English”) = True Then
     ‘Found an English Keyboard
     EnglishInput = InputLanguage.InstalledInputLanguages(i)
    End If
  End If
 Next i
End Sub
Private Sub TextBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
  InputLanguage.CurrentInputLanguage = ArabicInput
End Sub
Private Sub TextBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter
 InputLanguage.CurrentInputLanguage = EnglishInput
End Sub
 


I hope this helps 🙂


 

Comments (17)

  1. Mohamed says:

    First of all, thank you for blogging .

    Wouldn’t be better to restore the keyboard layout when leaving each edit box to what it was before entering it?

  2. I agree and this is what I suggested in Step4. The code is specific to the customer comment. Thanks!

  3. Ala'a B. Rasheed says:

    Thank you, that was very very useful

                                                       Best Regards.

  4. fazee says:

    how can we do this in ASP.NET WebForm

  5. Khaled says:

    Thanks it proved to be helpful

  6. Swetha says:

    Plz provide the same solution using Asp.net(with C#).

    As I need this urgently and I am unable to do.

    Thanks,

    Swetha

  7. Oussama says:

    thanks a lot you helped me a lot

    But there is a little error not in syntax

    "For i As Integer = 1 To (count – 1)"

    like this you misse the first item,It should be like

    "For i As Integer = 0 To (count – 1)"

  8. taymtogo says:

    the conditional statement should be so:

    InputLanguage.InstalledInputLanguages(i).LayoutName.Contains("(101)") = True Then

    because the layoutName is "Arabic" in an english version of Windows, in the French version of Windows the layoutName is "Arabe". This way i think that every diffrent language version of Windows has a specific layoutName, that’s why we must check the Code of the language who is UNIQUE.

    Very good tutorial 🙂

  9. Dave Hybrid says:

    Great post, really helped me understand, thanks!

  10. Siavash says:

    it was really useful

    thanks alot

  11. ABdelghani says:

    Thank you,

    that was very interesting!!!

  12. rohith kp says:

    i got count=1 only how can i install arabic language and other languages

  13. Ravi kumar says:

    Please provide the same solution in wpf

  14. Ganabo says:

    A great solution, but how to call it from any form – not this form –

  15. mateeullah aaqil says:

    thank you very much. i was so hasittd how to do so. thanks bro

  16. Asim says:

    How i can make a textbox control in c#,net that convert english language into urdu language at runtime ,while i have installed english language keyboard? what i should need to do?

  17. Ahmed Suror says:

    Thank you very very much, I was looking for this solution from a long time