[RESOLVED] [02/03] Reload form with different CultureInfo
During startup of my app, I get a string from a settings file that determines which language to display the form in, eg "en-GB", "fr-FR" etc, which works ok.
VB Code:
Public Sub ReadSettings()
'
'lang = "en-GB", "fr-FR", etc...
Dim ci As New System.Globalization.CultureInfo(lang, False)
System.Threading.Thread.CurrentThread.CurrentUICulture = ci
'
'etc
'
End Sub
My Sub Main:
VB Code:
Public Sub Main
ReadSettings()
Application.Run(New frmJobDisplay)
End Sub
The current thread is started using the culture determined from the settings file and the main form (which is localized) opens with the correct localized display. (Text and size of controls etc..)
My question is this: On the form I have a setting that changes the desired operator language and saves it to the settings file. To apply this change I currently have to exit the app and restart because all the resources for the form are read and applied on form load in the InitializeComponent sub.
What would be the best way to 'refresh' the form without exiting the app? I am assuming I must close the form and restart it somehow.
Thanks for any tips!
Re: [02/03] Reload form with different CultureInfo
You don't have to read the config file every time you want the values. The way most applications work, and the way application settings work in .NET 2.0, is that you read the existing values into variables at startup. You then use the values in those variables to control how your app behaves. If you need to change the behaviour you change the values in those variables. When the app shuts down you simply save the values from those variables to a file so that they are persisted to the next session. Reading and writing the file over and over is not what's usually done.
Re: [02/03] Reload form with different CultureInfo
Thanks for your reply.
However, I think I need to explain myself better.
Firstly, I am using .NET 1.1 with VB2003, so no .NET 2.0 or My.Settings for me. ;)
Secondly, the form I am referring to has its 'Localizable' property set to True. The form is altered in the IDE for each language I want to display by selecting the relevant 'Language' property from the dropdown box.
All the resource reading code is created and placed in the InitializeComponent sub by the IDE, which is then run when the form is opened, and the resources it uses depends on the culture setting I have read in from my settings file.
The only way I can get the form to display a new language is to close it (thus ending the app) and restarting. This is not a huge problem because quite honestly I would expect the operator language to be changed once in a blue moon, but it would be good if I could do it as soon as the setting is changed.
Re: [02/03] Reload form with different CultureInfo
I realise that you're not using .NET 2.0. I was just using that as an example of how .NET itself works, but I've never created a localised application so I have to admit that I don't know exactly what's involved and whether what I suggested would work in that case. It sounds like what you need is to be able to close your main form and open a new one without the application closing. It just so happens that I've posted code in the CodeBank to do exactly that.
http://www.vbforums.com/showthread.php?t=353165
Re: [02/03] Reload form with different CultureInfo
Thanks, I think that will do more or less what I want to do with a bit of playing around. Certainly the Do...Loop idea seems to be the way to go.
Just as a few bits of info...
I am using the localizing of the form because I am changing the size of controls depending on the language I need to display. (Some German translations I have are seriously long. I mean why use 5 letters for a word when 25 will do...)
If I was changing text ONLY, I would have used my own resource reader, as I am doing for strings that do not appear on a form, ie in Message boxes, etc.
If I change the CurrentCulture of the thread from withing the main form, and then open another sub form, this displays correctly because the culture property is set before the new form opens.