The Wayback Machine - https://web.archive.org/web/20150315023823/http://www.codeguru.com/columns/vb/switching-input-languages-dynamically-with-visual-basic.html

Switching Input Languages Dynamically with Visual Basic

Introduction

Hello and welcome to today's article. Today, I will show you how to change the input language dynamically.

First, the complicated stuff.

Globalization versus Localization

Globalization

Globalization is basically the process of making your application culture-friendly. You have to realize that the users of your application may speak a different language than you, and therefore their language adheres to different rules, apart from being a different language. For example, some languages are displayed left to right, while others should be displayed right to left. Now, you have to not only think about the different language, but also different currencies, date and time settings, as well as different colours being used with different cultures.

With Globalization, you can make your application useable with any culture, irrespective of their language.

Here are some more articles on Globalization:

Localization

Localization allows you to customize your application according to a certain culture. Let's do a small project!

Design

Design your application to resemble Figure 1.

Design
Figure 1: Our Design

Set the Tag property of each of the Textboxes to languages of your choice. I chose English and Afrikaans (which is my native language).

Code

As usual, add the Declarations first:

Private Const _ENGLISH As String = "ENGL"
Private Const _AFRI As String = "AFRI"

These constants will help identify which languages we want to use. Obviously, you may use your own languages.

Add the following piece of code to dynamically change your input type to a different language, once the focus has been set to either one of the textboxes:

'Method to handle GotFocus events for both textboxes
Private Sub TextBoxes_GotFocus(ByVal sender As Object, _
   ByVal e As System.EventArgs) _
   Handles txtNameEnglish.GotFocus, txtNameAfri.GotFocus

   'We'll use the case statement to inspect the sender object's
   '.Tag property.
   Select Case sender.Tag

      Case _AFRI   'If the Tag contains the word "AFRi"

      'Loop through all the installed languages on this system.
      For Each Lng As InputLanguage In _
         InputLanguage.InstalledInputLanguages

         'If there exists a language whose DisplayName has the
         'word "AFRI" in it
         If Lng.Culture.DisplayName.ToUpper.StartsWith(_AFRI) Then

            'Change current input language to that
            InputLanguage.CurrentInputLanguage = Lng

            'Exit for - coz we have found our language and no need
            'to go through the rest of the loop
            Exit For

         End If

      Next

   Case _ENGLISH   'If the Tag contains the word "ENGL"

      'Loop through all the installed languages on this system
      For Each Lng As InputLanguage In _
         InputLanguage.InstalledInputLanguages

         'If there exists a language whose DisplayName has the
         'word "ENGL" in it
         If Lng.Culture.DisplayName.ToUpper.StartsWith(_ENGLISH) Then

            'Change current input language to that
            InputLanguage.CurrentInputLanguage = Lng

            'Exit for - coz we have found our language and no need
            'to go through the rest of the loop
            Exit For

         End If

      Next

   End Select

End Sub

All that happens here is the following:

  • We make use of the GotFocus event for the TextBoxes. You could actually have used the LostFocus event as well, depending on when you want this to fire. Notice the Handles clause handles both textboxes.
  • I determine the current object's Tag with the use of a Select Case statement.
  • I started a For Loop to loop through all of the installed languages on the machine.
  • With a little string manipulation, I determine what the current installed language starts with. If it starts with either of our languages, then,
  • Make use of InputLanguage to change the language dynamically.

Here is some more information on InputLanguage:

Here is some more information the CultureInfo object:

https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo%28v=vs.110%29.aspx

I have attached a working sample along with this project for you to play with.

Conclusion

Thank you for reading my article. Until we meet again, this is me signing off. Bye!



Related Articles

Downloads

Comments

  • Swift Methods For manage my google alerts - For Adults

    Posted by Lucia Baier on 03/10/2015 02:31am

    With ad groups which are more tightly focused it is possible to quickly spot poor performers and react accordingly by lowering bids, adding various negatives, adjusting match type or writing new ad copy. If you want to find out if keywords and phrases are supplying you with accomplishment and add some new ones, you are able to talk about the Google Keyword tool. Exact match - applying this type, you need to apply the square brackets [ ] around the search term which narrows the search even further.

    Reply

Top White Papers and Webcasts

  • Who can you trust? Learn from the IBM X-Force team in this new quarterly report how the Internet of Things and IP reputation tracking are transforming the security landscape.

  • Providing superior IT solutions not only differentiate VARs from their competitors, this practice also establishes a foundation of trust upon which a VAR can build a lasting advisor relationship with its customers. VARs who settle for mediocre AV products ultimately encourage customers to view the VAR as equally undistinguished. Delivering truly exceptional endpoint security – thus building credibility and trust with the VAR’s customers – demands a fundamentally new approach. Read this white …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date