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.


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.

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

3) Add your panels and content.

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

hiddenPanel.Visible = false;

隐

5) or true

hiddenPanel.Visible = true;

可见


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 (eg, if the panel is larger than your usercontrol or has other controls beside your usercontrol.)


If you'd like to resize it to a particular size, you can do it on the code behind:

Size panelSize = new Size(500, 500);
usercontrol1.Parent.Size = panelSize;

You could add this code to the usercontrol if that is where you wish to resize from.

链接地址: http://www.djcxy.com/p/61998.html

上一篇: 在UserControl中模拟TrackBar调整行为

下一篇: 调整包含UserControl的面板的大小