Results 1 to 5 of 5

Thread: [RESOLVED] [02/03] Reload form with different CultureInfo

  1. #1

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Resolved [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:
    1. Public Sub ReadSettings()
    2. '
    3. 'lang = "en-GB", "fr-FR", etc...
    4. Dim ci As New System.Globalization.CultureInfo(lang, False)
    5. System.Threading.Thread.CurrentThread.CurrentUICulture = ci
    6. '
    7. 'etc
    8. '
    9. End Sub
    My Sub Main:
    VB Code:
    1. Public Sub Main
    2. ReadSettings()
    3. Application.Run(New frmJobDisplay)
    4. 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!
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    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.
    Last edited by Andy_P; Sep 11th, 2006 at 01:16 PM.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    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.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width