0

I am developing a windows form Application.

In my Main form I have a menu item and on click that menu item I need to change the language of the whole form.

I am setting the UICulture to new Culture still the new language is not showing.

As a work around I need to iterate over all controls in that form and apply new resource. I don't like this at all.

For some other problem I canot do Application.Restart(); Also my form is very heavy so I don't want hide the form and create a new one.

So is there any alternatives to reload my form?

gmtek
  • 741
  • 3
  • 7
  • 25
  • Possible duplicate of http://stackoverflow.com/questions/7556367/how-do-i-change-the-culture-of-a-winforms-application-at-runtime – Csaba Toth Jul 24 '13 at 20:08
  • Do you need to persist the state of the controls? – RJ Programmer Jul 24 '13 at 20:11
  • yes I need to persist the state also. – gmtek Jul 24 '13 at 20:12
  • 1
    No its not duplicate.I don't want to iterate over the controls. – gmtek Jul 24 '13 at 20:14
  • I don't know other solution by myself. What's the exact problem with reload? Culture change is not a frequent thing, so even if UI flickers a little because of the reload the user could bear with it. – Csaba Toth Jul 24 '13 at 20:27
  • According to the requirement the frequency of culture change is 10/Day. And also There are lots of control on that form and the device is a stand alone .NET system with 256 MB of memory So I am trying to avoid this iteration if possible. – gmtek Jul 24 '13 at 20:37

3 Answers3

2

Maybe it will work for you.

To the clicking event or any

this.Controls.Clear();
  InitializeComponent();
Emre
  • 97
  • 8
  • I cannot format the code in your answer because the edit is to small to submit, so please format the code properly yourself. – Alexey Subach Mar 23 '17 at 20:30
1

So, based on what you want to do, I think the only way you could avoid iterating the entire form components collection, is by iterating over the culture specific resources and applying them to the controls. This should be a smaller set since only the subset of controls on the form would have values that need to change. It would look something like this:

var enumerator = mgr.GetResourceSet(Thread.CurrentThread.CurrentUICulture, true, true).GetEnumerator();

while (enumerator.MoveNext())
{
  var current = enumerator.Entry;
  var objName = current.Key.ToString().Split('.')[0];
  var item = this.GetType().GetField( objName, BindingFlags.NonPublic | BindingFlags.Instance );

  if (item != null)
  {
      mgr.ApplyResources(item.GetValue(this), objName);
  }
}

Notice that rather than iterating over the forms control collection, this is iterating over the keys in the resource file, using reflection to find the component on the form and applying resources only to those components that have settings in the file.

This is only a sample and I think it's rather brittle (i.e. nested gridview components that do not have a corresponding field on the form. Names that have multiple "." levels in a resource file, etc.) but it should be enough to get you started, I hope!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
RJ Programmer
  • 788
  • 5
  • 7
  • Also found this, which may be a more stable solution: http://www.codeproject.com/Articles/23694/Changing-Your-Application-User-Interface-Culture-O#UsingTheComponent – RJ Programmer Jul 24 '13 at 22:01
0

I have found another alternative.

Instid of directing adding controls to form I am adding all controls to a userControl. So my main form only contains one Usercontrol.

Now when changing language I just remove the user control, dispose it, create a new user control and add it to the form.

What do u think guys about this approach?

gmtek
  • 741
  • 3
  • 7
  • 25