Jump to content



Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!

Toggle shoutbox Shoutbox Open the Shoutbox in a popup

@  agyat : (26 May 2013 - 08:25 AM) And Need Some Suggestion To Start A Ball Roll Here.
@  agyat : (26 May 2013 - 08:24 AM) Nothing Special For The Day. What About You?
@  yordan : (25 May 2013 - 09:50 PM) Hi, Agyat What's New?
@  yordan : (25 May 2013 - 07:59 PM) Yay, Shoutbox Is Back! Yahooo!
@  agyat : (24 May 2013 - 05:15 PM) O Dear, Where Are You? Without Your Words This Sb Is ..
@  agyat : (23 May 2013 - 01:23 AM) Wow! Mr. Sb Back Home.
@  OpaQue : (23 May 2013 - 12:44 AM) Ting
@  OpaQue : (24 April 2013 - 02:44 PM) I guess, Time to run Mycent script.
@  OpaQue : (24 April 2013 - 02:43 PM) wow.. not much spam. except habatt posting lot of links.. :P
@  yordan : (23 April 2013 - 01:04 PM) You're welcome, agyat. Nice to have been helpful. Second lesson: try full words, "you" instead of "EW".
@  agyat : (23 April 2013 - 05:03 AM) @YORDAN: tHANK EW FOR YOUR FIRST LESSON.   :D
@  yordan : (22 April 2013 - 09:43 PM) @agyat : "why don't you help me", or "please help me", or "please teach us"
@  yordan : (22 April 2013 - 09:42 PM) welcome back, velma
@  velma : (22 April 2013 - 07:51 AM) **yawns** Good to be back, wonder what is going on here :)
@  agyat : (22 April 2013 - 03:50 AM) Oh! so, why don't help me learn english..
@  yordan : (21 April 2013 - 08:38 PM) The goal mentioned by shiu : "learning english, learning computer"
@  agyat : (21 April 2013 - 06:31 PM) WHAT GOAL?
@  yordan : (20 April 2013 - 10:39 AM) yes, that's our goal. simultaneouly learning English and teaching/learning computer using.
@  shiyu : (20 April 2013 - 07:30 AM) learning english,learning computer
@  yordan : (19 April 2013 - 01:11 PM) Oh, I see, it's just a trick in order to force people looking at your texte. Somehow smart, maybe.

Photo
- - - - -

VB.NET: Switch Regional Language Automatically


5 replies to this topic

#1 miCRoSCoPiC^eaRthLinG

miCRoSCoPiC^eaRthLinG

    PsYcheDeLiC dR3aMeR

  • [MODERATOR]
  • 2,248 posts
  • Gender:Male
  • Location:Bangkok, Thailand
  • Interests:Photography, Magic Tricks, Numismatics & Philately to some extent, Being a nuisance in general (that's my favourite)
  • myCENTs:NEGATIVE[-21.50]

Posted 14 June 2005 - 12:22 PM

Switch the language of the Textbox Control automatically upon receiving focus

Hi guys,
This tutorial sparked off from my own ventures to incorporate a particular feature in my own software - which uses mixed language (English & Thai) on a couple of screens to store user data. Certain fields, apart from First & Last Names & parts of address were to be entered in Thai. Now the current language can be easily switched by pressing Alt-Left Shift (or whatever key combination you've set your system to) - but when you consider the same key-combination has to be repeated for over 20 fields on each screen - and this times the number of records you have to enter, then is becomes a dull and humongously tedious venture. This might lead to a lot of typo's too. (In my case the users of my software had to enter over 4000 student records - soooooo... I had to do something to make the lives of those poor guys a little easier.)

So I thought, why not let the software do the language switching instead. I mean, the fields which were supposed to be in english and thai - were fixed, i.e. you couldn't use these languages alternatively.. so why not, let the system switch to Thai upon focus on such a thai data entry field and similarly for English ? So here I am with the solution - for anyone who might face such a situation (which I believe is very very common). This code will work for any language combination (or even more than two languages) with extremely minor modifications. Of course, you HAVE TO HAVE all these language packs installed on Windows to be able to switch back and forth between them.


First things first: One convention I found very useful in achieving this effect, is to include part of the language name in the Name property of Textbox - this can then be used to easily determine which language to switch to in the corresponding textbox. Following this, I named all my Thai textboxes ending with the word "Thai" and "Engl" for the English textboxes. In reality, this is irrelevant. You can name your textboxes to whatever you feel like, as there is a better way to do it. Each control in .NET has a very little used property called .Tag - which can be used to store any kind of information about that control for internal use. We'll use this .Tag property to store the language of the control.


Step 1
Let us start by firing up the VS.NET Studio & creating an empty VB.NET project with just one form. Add at least TWO textboxes to this form (feel free to add more and experiment around with them - but for demonstration's sake, am going to use just two).

Now, in this example we'll use two Constants to store the words "ENGL" and "THAI" - so that we don't have to rewrite these words on every instance, lessening our chances of error. Let's consider these two words as nicknames to refer to these languages.
   'Constant for english language
    Private Const _ENGLISH As String = "ENGL"
    'Constant for thai language
    Private Const _THAI As String = "THAI"

    'A Temporary variable to hold the color of a control
    Private OriginalColor As Color

In the Property Inspector enter txtNameThai as the name of the first box, and txtNameEnglish for the second one.
AND, the real part:
  • Set the .Tag property of txtNameEnglish to "ENGL"
  • Set the same for txtNameThai to "THAI"
That's it - the stage is set. Onto the actual code now.


Step 2
Go to the Code View of the form and create the empty skeleton procedures for the Focus event of the first textbox. When you first create a skeleton event handler procedure, using the dropdown boxes on top of the editor, the basic structure looks like this:
Private Sub txtNameThai_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) _
                        Handles txtNameThai.GotFocus
                        
End Sub

Now, supposing we have over 20 different textboxes on the form - writing the language switching code for ALL of them is tedious and produces useless, redundant code. So we'll point the event handlers for ALL the textboxes to this one procedure. I simply modified the code a triffle bit to handle the focus event for the second textbox too. READ THE COMMENTS.
' Modified code to handle focus events of ALL textboxes
' Notice how I simply added the the focus event of the second textbox after the Handles clause
' If you have more than two textboxes, add the names of all of them here separated by commas.
    Private Sub TextBoxes_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) _
                Handles txtNameEnglish.GotFocus, txtNameThai.GotFocus
                 
                        
End Sub


Next steo is to incorporate the actual code to switch languages. Since this subroutine handles the focus events of multiple textboxes, we'll use the arguement sender to determine which control has current focus - and also determine the language to be switched to using the same.

Add this code inside the sub:
   'Method to handle GotFocus events for both textboxes
    Private Sub TextBoxes_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) _
                Handles txtNameEnglish.GotFocus, txtNameThai.GotFocus


        '===========================================================================
        '                   FIRST PART - LANGUAGE SWITCHING
        '===========================================================================
        'We'll use the case statement to inspect the sender objects .Tag property
        Select Case sender.Tag

            Case _THAI              'If the Tag contains the word "THAI"

                '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 got the 
                    'word "THAI" in it
                    If Lng.Culture.DisplayName.ToUpper.StartsWith(_THAI) 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 got 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

A little explanation: I'm using the sender.Tag property to bring up the name string of the currently focused control. Remember, how I emphasized on setting the Tag properties for both to the nicknames we chose for each of the languages - notice how it comes to handy here.

If sender.name.endswith("Thai") - determines if the control's name endswith the string "Thai". If found so, we step into the next line, which is a For Each loop.
Now we can skip this line and switch the language directly, but a better way would be to use the For..Each loop, as this implements an extra check for the installed languages on the system. This safeguards the case, where the desired language is missing from the system - and block the possibilities of runtime errors when the code tries to switch to a non-existant language. READ THE COMMENTS.
       Select Case sender.Tag

            Case _THAI              'If the Tag contains the word "THAI"
	
                '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 got the 
                    'word "THAI" in it
                    If Lng.Culture.DisplayName.ToUpper.StartsWith(_THAI) 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

	'English code follows exactly same syntax - except that THAI is replaced by ENGL

End If

If you need to switch between more than two languages, you need to implement more cross-checks by using more than two CASE statements for the .Tag property and implement appropriate language switching cycles in each case.


Another small trick than you can add to this to enhance your interface is - change the background color of the textbox which has current focus. This allows the user to follow the field being currently edited in a much easier manner. All you've to do, is in this same GotFocus procedure, you add a couple of more lines after switching the language. This code Saves the current back color of the control and switches it to our desired color.
  '===========================================================================
        '                   SECOND PART - COLOR SWITCHING
        '===========================================================================
        'First Store the Original Color
        OriginalColor = sender.BackColor

        'Simply switch the back-color of the sender
        sender.BackColor = Color.PapayaWhip

Remember, if you use this trick - you'll also have to include the LostFocus procedure which will reset the textboxe's back-color to white upon loosing focus - else all your textboxes will end up with the focus color as you navigate through them.
       'Method to turn the fields white on lostfocus
        Private Sub TextFields_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) _
                         Handles txtNameThai.LostFocus, txtNameEnglish.LostFocus

        '===========================================================================
        '                  COLOR SWITCHING - BACK TO THE ORIGINAL
        '===========================================================================
        'Switch the back color to the original one
        sender.BackColor = OriginalColor

        End Sub
As demonstrated here - this code switches the back-color to the original color upon loosing focus - and once again handles all your textfield in a single procedure.


Hope these two nifty tricks help you enliven your application - I for one find them very very useful - and from personal experience, these make the users happy too - as they have to use far less keys.

Comments & Feedbacks much appreciated. :(

Attached File: Sample working project containing the above code.Attached File  AutoLanguageSwitcher.zip   27.15K   168 downloads

#2 Guest_FeedBacker_*

Guest_FeedBacker_*
  • Guests

Posted 06 June 2008 - 03:32 PM

amazing
VB.NET: Switch Regional Language Automatically

I really want it because I need this this code for changing language in my software



Thanks

-feedback by moon

#3 Guest_(G)chris_*

Guest_(G)chris_*
  • Guests

Posted 22 February 2009 - 08:19 PM

I like this article, but I don't understand why wouldn't you just use globalization to do this, globalization would allow me to do thiswith many languages. Check out this guys website which shows multilinguage language for vb.Net with thai language.

-reply by chris

#4 Guest_(G)Dick_*

Guest_(G)Dick_*
  • Guests

Posted 13 May 2010 - 10:55 AM

Good postVB.NET: Switch Regional Language Automatically

First - respect for this great post - it addresses an issue that is quite complex as well as providing a logical solution for a quirky problem I was having with the language setting switching when I wasn't expecting the keyboard switch.  Apparently in my case I was inadvertently hitting Alt-shift sometimes and causing the language to shift out of the one I wanted to be using - sometimes frustrating the hell out of me as I was entering a password and not realizing the keyboard language shift had happened!!  Anyway, I went back into regional settings and took the check-marks off from the key-sequence setting for switching and now only switch with the mouse.

I will come back to this page when the more complicated problem of multilingual data entry becomes a need in the future!

 THANK YOU FOR THE NIFTY TRICKS AND INSPIRATION TO SOLVE MY PROBLEM!

 


 

-reply by ****


#5 Guest_(G)Garouda_*

Guest_(G)Garouda_*
  • Guests

Posted 02 October 2011 - 02:51 AM

How to contact you?VB.NET: Switch Regional Language Automatically

Replying to miCRoSCoPiC^eaRthLinGHi, I am new on this site. I've been working in Thailand for more than seven years now. In April 2010 I started to write a comprehensive VB6 application to manage all aspects of the management of a school, except for finance as this isn't in my field. I am using VB6, ADO and Microsoft Jet as database (the Access Engine - I don't use Access itself).I am now in the process of upgrading from VB6 to dot Net and it's a little nightmare. I like your code snippet and would like to share some experience with you.

Best Wishes,B.-reply by Garouda



#6 Guest_Faraz Ismail_*

Guest_Faraz Ismail_*
  • Guests

Posted 14 September 2012 - 10:39 PM

HELLO

Thanks a lot for sharing this code I was trying to solve this problem from a long time but have no success...



Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users