In the WinForms application, a Panel used as a placeholder to display a single user control as a navigation strategy: whenever a user wants to go to this area, the corresponding user control is added to the panel. Simplified:
contentPanel.Controls.Clear(); userControl.Dock = DockStyle.Fill; contentPanel.Controls.Add(userControl);
As a result of a requirement that is not under my control, the form must support dynamic language switching. This is implemented and works fine using the Hans Passant answer , with a modification to using the user management resource manager, which correctly receives and applies localized text to the controls.
However, after applying resources from the user control resource file, the layout obtained as a result of DockStyle.Fill is lost for user control elements that are not themselves configured to have DockStyle.Fill . This leads to the fact that the controls are no longer stretched to fill the available area, and are limited to the original size defined in the constructor / resource file. Note that the Dock User Control property is still correctly set to DockStyle.Fill after applying resources.
I created an example application that illustrates / reproduces the problem: in the form below there is a panel to which the user control is added dynamically and installed on DockStyle.Fill . The user control has a label that is pinned in the upper left corner of the default locale and in the upper right corner in German. I would expect the form to snap a label anchored to the right of the right field of the form, but the size of the reset user control is equal to the value at design time. View source code .


If I run the form in German, the label is correctly laid out against the right edge of the form:

I would like the layout to be saved after calling ApplyResources . Of course, I could just make a copy of the Location and Size control properties (as suggested in another answer to the same question mentioned above), but, unfortunately, the values โโof these properties differ between locales. So, after the localized string and positioning are applied, how can the user control be redirected to all its controls again?
What i tried
- Studying
InitializeComponent() , I tried to call PerformLayout() in the Panel container, User Control and the form to no avail. - Adding
SuspendLayout() and ResumeLayout(true) before and after the ApplyResources call is also unsuccessful.
Additional Implementation Details
- Links to user-created user controls are stored in a private dictionary in the main form. When the navigation for this control is raised, the previous user control is deleted, and the existing link is added with the snippet above.
Response to a custom language change event:
protected virtual void OnChangeCulture(CultureInfo newCulture) { System.Threading.Thread.CurrentThread.CurrentCulture = newCulture; System.Threading.Thread.CurrentThread.CurrentUICulture = newCulture; SuspendLayout(); ComponentResourceManager resources = new ComponentResourceManager(this.GetType()); ApplyResources(resources, this, newCulture); ResumeLayout(true); }
The use of resources for all controls in the form:
private void ApplyResources(ComponentResourceManager resourceMgr, Component target, CultureInfo culture) {
Thanks a lot in advance for any pointers!