Resizing inline user controls in C # panel

Using WinForms in C #, I'm struggling to correctly resize the inline user control. I can add it to the panel without any problems, and the panel will resize as I expect (and want).

To add a UserControl, I do the following:

content.Controls.RemoveAt(0); content.Controls.Add(c); content.Controls[0].Dock = DockStyle.Fill; content.Refresh(); 

I can’t figure out how to properly configure the newly added control, I also tried using Anchor with Top, Bottom, Left, Right, but to no avail. Any help would be appreciated, thanks.

+4
source share
3 answers

Adding below will resize the user control using the size of the parent control.

 this.Dock = DockStyle.Fill; 

But if you want to resize child user controls, you will need to correctly set the dock values ​​and binding values. Otherwise, the main user control will resize, but the internal child controls of the user will remain as they are.

You said:

<<control moves down instead of resizing

If there are some child controls that you want to stretch vertically, you can try the FlowLayout panel.

+2
source

Are you 100% sure that Controls [0] actually refer to your control after adding?

Your local var for the control is still valid, you can verify that you are docking the dock c.Dock = DockStyle.Fill;

+2
source

I get it.
In the panel that is the base for all added panels, add a handler for the Resize event.
On OnResize() add:

  private void SummaryData_Resize(object sender, EventArgs e) { foreach (MyPanel pan in this.Controls) { pan.Dock = DockStyle.Fill; } } 

It worked for me. I debugged the code, and it seems that in OnResize all my panels lost the Dock setting.

+1
source

All Articles