简体   繁体   English

调整包含UserControl的面板的大小

[英]Resizing Panel Containing UserControl

I have a UserControl that contains invisible controls, to make them visible, the UserControl resizes. 我有一个包含不可见控件的UserControl,为使其可见,需要调整UserControl的大小。

I need to resize the Panel that contains the UserControl, but I don't know how. 我需要调整包含UserControl的面板的大小,但是我不知道如何。

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. Panel和Form类可以很好地处理此行为,而无需显式调整大小(并且当用户使用高DPI监视器或使用大字体或超大字体设置时,也不会引入布局错误)。

1) Create a Form with a docked FlowLayoutPanel. 1)使用停靠的FlowLayoutPanel创建一个表单。

停靠

2) Set the Form and FlowLayoutPanel's AutoSize to true and AutoSizeMode to GrowAndShrink 2)将Form和FlowLayoutPanel的AutoSize设置为true,将AutoSizeMode设置为GrowAndShrink

成长与收缩

3) Add your panels and content. 3)添加面板和内容。

设计

4) Programmatically set the desired panel's Visible property to hidden 4)以编程方式将所需面板的Visible属性设置为hidden

hiddenPanel.Visible = false;

隐

5) or true 5)或是

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.) 还将保留边距(例如,如果面板大于您的用户控件或除用户控件之外还有其他控件)。

To resize the control , call scale of to the control. 要调整控件的大小,请调用控件的scale。

           // To zoom in controls.
            foreach (Control c in MyFlowLayoutPanel.Controls)
            {
                PictureBox ptc = c as PictureBox;

                if (null != ptc)
                {
                    Point pt = new Point(2, 2);
                    SizeF sf = new SizeF(pt);
                    c.Scale(sf);
                }
            }

// To zoom out controls. //缩小控件。 foreach (Control c in MyFlowLayoutPanel.Controls) { PictureBox ptc = c as PictureBox; foreach(MyFlowLayoutPanel.Controls中的控件c){PictureBox ptc = c as PictureBox;

                                if (null != ptc)
                                {
                                    SizeF sf = new SizeF(0.5F, 0.5F);
                                    c.Scale(sf);
                                }
                            }

I know that this topic is pretty old but I want to add my method, as well... 我知道这个主题已经很老了,但是我也想添加我的方法...

If you have a Panel containing a UserControl you can easially resize the panel.Controls by firing Form1_Resize event. 如果您的面板包含一个UserControl,则可以通过触发Form1_Resize事件轻松地调整panel.Controls的大小。

 private void Form1_Resize(object sender, EventArgs e)
        {
            foreach (Control control in MasterPanel.Controls)
            {
                control.Size = MasterPanel.Size;
            }

        }

Just make sure that you anchor its content properly. 只要确保您正确锚定其内容即可。

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. 如果要从中调整大小,则可以将此代码添加到用户控件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM