次の方法で共有


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
PrivateSub 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

  • Anonymous
    April 28, 2007
    The comment has been removed

  • Anonymous
    April 29, 2007
    I agree and this is what I suggested in Step4. The code is specific to the customer comment. Thanks!

  • Anonymous
    November 05, 2007
    Thank you, that was very very useful                                                    Best Regards.

  • Anonymous
    March 04, 2008
    how can we do this in ASP.NET WebForm

  • Anonymous
    April 27, 2008
    Thanks it proved to be helpful

  • Anonymous
    July 09, 2008
    Plz provide the same solution using Asp.net(with C#). As I need this urgently and I am unable to do. Thanks, Swetha

  • Anonymous
    August 02, 2008
    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)"

  • Anonymous
    August 21, 2008
    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 :)

  • Anonymous
    September 18, 2008
    Great post, really helped me understand, thanks!

  • Anonymous
    January 18, 2010
    it was really useful thanks alot

  • Anonymous
    March 02, 2011
    Thank you, that was very interesting!!!

  • Anonymous
    April 06, 2013
    i got count=1 only how can i install arabic language and other languages

  • Anonymous
    July 11, 2013
    Please provide the same solution in wpf

  • Anonymous
    April 15, 2014
    A great solution, but how to call it from any form - not this form -

  • Anonymous
    July 07, 2014
    thank you very much. i was so hasittd how to do so. thanks bro

  • Anonymous
    November 17, 2015
    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?

  • Anonymous
    December 17, 2015
    Thank you very very much, I was looking for this solution from a long time