Change the language of a form instantly?

Hi!

Is there a way to change the language of a form instantly e.g. When I click a button I want to change the Text of all controls from English to German.

Thanks.
Jus

 



Answer this question

Change the language of a form instantly?

  • iron94

    Dear Jus,

    I have used a radiobutton instead of a text box. You can change it.

    // Event

    rGerman.CheckedChanged += new System.EventHandler(rGerman_CheckedChanged);

    private void rGerman_CheckedChanged(object sender, System.EventArgs e)
    {
     // Make the CultureInfo.
     CultureInfo culture_info = new CultureInfo("de-DE");

     // Make a ComponentResourceManager.
     ComponentResourceManager component_resource_manager = new ComponentResourceManager(this.GetType());

     // Save the current location.
     Point old_location = this.Location;

     // Apply resources to the form.
     component_resource_manager.ApplyResources(this, "$this", culture_info);

     // Restore the location.
     this.Location = old_location;

     // Apply resources to the form's controls.
     foreach (Control ctl in this.Controls)
     {
      component_resource_manager.ApplyResources(ctl, ctl.Name, culture_info);
     }
    }

     

    HTH,

    Suprotim Agarwal



  • bmc410

    This question is related to Windows Forms and not C#.  You should, in the future, post it in the Windows Forms forum.  You will find that you'll probably get a quicker, better response there.  Thanks.

    If the form is set to be localized in the designer then its contents are stored in a locale-specific format.  The runtime is responsible for finding the right locale and loading it when the form loads.  As for changing it on the fly you could in theory change the culture of the current thread using Thread.CurrentThread.UICulture and then invalidate the form.  I'm not sure if the runtime will load the form with the new culture or will require you to destroy and recreate the form first. 

    If you don't want to do that then I'd recommend that the text of your controls come from a centralized resource manager which gets the string based on some culture property (either the thread's or your own custom one).  Invalidating the form should cause the strings to be reloaded with the correct language.  .NET's resource manager class might do this for you automatically (again provided you are using the thread's UICulture).

    Michael Taylor - 12/16/05


  • Change the language of a form instantly?