Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing Panel Containing UserControl

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.

like image 958
mggSoft Avatar asked Aug 31 '26 20:08

mggSoft


2 Answers

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

like image 162
Ritch Melton Avatar answered Sep 02 '26 12:09

Ritch Melton


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.)

like image 20
FastAl Avatar answered Sep 02 '26 11:09

FastAl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!