Search code examples
c#winformsuser-controls

how to set responsive width for user control in win forms


I'm working on a windows form application and I have a parent form I call the user controls through it, the problem is when the parent form is maximized the user control appears well enter image description here

but when I minimize the parent form I see that a part of the user control becomes hidden.

enter image description here

although I show the user control inside the panel and I set the dock for this panel to Fill.

and I use the following code to show that user control inside the panel:

        xUc.Dock = DockStyle.Fill;
        this.PnlView.Controls.Clear();
        this.PnlView.Controls.Add(xUc);
        xUc.BringToFront();

Updtae1: I used the TableLayoutPanel in the user control and I faced the same problem:

Minimized Form: enter image description here

Maxmized Form: enter image description here


Solution

  • The screenshots denote the two panels are not hosted by the same parent. You can verify that by checking what do you have in the PnlView.Controls collection or by checking the Parent property of the left and right panels. So, xUc.Dock = DockStyle.Fill; does not give you the expected dock layout, it fills the free space of the PnlView.

    To get the expected layout, make sure to host the two panels by the same parent, if that is not an option (for example, the right panel is some sort of floating panels) then handle the relevant events (i.e. Parent.SizeChanged, RightPanel.VisibleChanged...etc) to set the bounds (mainly the width) of the left panel.