Resizing Panel Containing UserControl

15,336

Solution 1

This behavior is handled well by the Panel and Form classes without explicit sizing (and without the layout bugs introduced when the user has a high-DPI monitor or uses the large or extra-large font settings.

1) Create a Form with a docked FlowLayoutPanel.

Docked

2) Set the Form and FlowLayoutPanel's AutoSize to true and AutoSizeMode to GrowAndShrink

GrowAndShrink

3) Add your panels and content.

Design

4) Programmatically set the desired panel's Visible property to hidden

hiddenPanel.Visible = false;

Hidden

5) or true

hiddenPanel.Visible = true;

Visible

Solution 2

Put this code in the usercontrol:

Size last = new Size(0, 0);

private void Me_Resize(object sender, System.EventArgs e)
{
    if (last != new Size(0, 0)) {
        this.Parent.Size = Size.Add(this.Parent.Size, Size.Subtract(this.Size, last));
    }
    last = this.Size;
}

Will also retain margins (e.g., if the panel is larger than your usercontrol or has other controls beside your usercontrol.)

Share:
15,336
mggSoft
Author by

mggSoft

Updated on June 25, 2022

Comments

  • mggSoft
    mggSoft almost 2 years

    I have a UserControl that contains invisible controls, to make them visible, the UserControl resizes.

    I need to resize the Panel that contains the UserControl, but I don't know how.