C# 内部带有flowlayout面板且autosize=true的Groupbox会收缩,就像它是空的一样
我有一个groupbox,其中包含一个flowlayout面板,flowlayout面板包含一组控件。我将flowlayout面板设置为与父级对接。因为我不知道面板中会有多少个控件,所以我将组框autosize设置为true,将autosizemode设置为增长和收缩。当我这样做时,groupbox会收缩,就像它是空的一样。我需要标题,因此无法删除groupbox。有人知道为什么会这样吗 您声明“我不知道面板中将有多少控件”。在设计时,FlowLayoutPanel中是否有任何控件?如果你不这样做,这听起来像是预期的行为。面板没有任何内容,因此其所需大小为零,因此GroupBox的所需大小为零C# 内部带有flowlayout面板且autosize=true的Groupbox会收缩,就像它是空的一样,c#,autosize,groupbox,flowlayoutpanel,C#,Autosize,Groupbox,Flowlayoutpanel,我有一个groupbox,其中包含一个flowlayout面板,flowlayout面板包含一组控件。我将flowlayout面板设置为与父级对接。因为我不知道面板中会有多少个控件,所以我将组框autosize设置为true,将autosizemode设置为增长和收缩。当我这样做时,groupbox会收缩,就像它是空的一样。我需要标题,因此无法删除groupbox。有人知道为什么会这样吗 您声明“我不知道面板中将有多少控件”。在设计时,FlowLayoutPanel中是否有任何控件?如果你不这样
如果是这种情况,那么当您在运行时实际添加控件时,它的大小应该会增大。没有任何东西可以阻止FlowLayoutPanel缩小为零。您至少还必须将其AutoSize属性设置为True。您可以为groupBox设置属性锚定:顶部、底部、左侧和右侧。我今天也在尝试做同样的事情。下面是我提出的解决方案,它是将FlowLayoutPanel停靠在GroupBox内部,然后使用FlowLayoutPanel的Resize和ControlAdded事件触发调整父GroupBox的大小 调整大小处理程序在FlowLayoutPanel中查找最后一个控件的底部,并调整GroupBox的大小,使其具有足够的空间来容纳FlowLayoutPanel中最底部的控件 我尝试在FlowLayoutPanel和GroupPanel上使用AutoSize=true。但不幸的是,这允许FlowLayoutPanel水平增长
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
int numGroupBoxes = 4;
for (int groupBoxIndex=0; groupBoxIndex<numGroupBoxes; groupBoxIndex++ )
{
GroupBox groupBox = new GroupBox();
groupBox.Text = "Group " + groupBoxIndex;
groupBox.Size = new Size(this.Width, 0);
groupBox.Dock = DockStyle.Top;
this.Controls.Add(groupBox);
FlowLayoutPanel groupBoxFlowLayout = new FlowLayoutPanel();
groupBoxFlowLayout.Dock = DockStyle.Fill;
groupBox.Controls.Add(groupBoxFlowLayout);
int extraSpace = 25; // the difference in height between the groupbox and the contents inside of it
MethodInvoker resizeGroupBox = (() =>
{
int numControls = groupBoxFlowLayout.Controls.Count;
if ( numControls > 0 )
{
Control lastControl = groupBoxFlowLayout.Controls[numControls - 1];
int bottom = lastControl.Bounds.Bottom;
groupBox.Size = new Size(groupBox.Width, bottom + extraSpace);
groupBoxFlowLayout.Size = new Size(groupBoxFlowLayout.Width, bottom);
}
});
groupBoxFlowLayout.Resize += ((s, e) => resizeGroupBox());
groupBoxFlowLayout.ControlAdded += ((s, e) => resizeGroupBox());
// Populate each flow panel with a different number of buttons
int numButtonsInGroupBox = 3 * (groupBoxIndex+1);
for (int buttonIndex = 0; buttonIndex < numButtonsInGroupBox; buttonIndex++)
{
Button button = new Button();
button.Margin = new Padding(0, 0, 0, 0);
string buttonText = buttonIndex.ToString();
button.Text = buttonText;
button.Size = new Size(0,0);
button.AutoSize = true;
groupBoxFlowLayout.Controls.Add(button);
}
}
}
}
公共部分类UserControl1:UserControl
{
公共用户控制1()
{
初始化组件();
int numgroupbox=4;
对于(int groupBoxIndex=0;groupBoxIndex)
{
int numControls=groupBoxFlowLayout.Controls.Count;
如果(numControls>0)
{
Control lastControl=groupBoxFlowLayout.Controls[numControls-1];
int bottom=lastControl.Bounds.bottom;
groupBox.Size=新尺寸(groupBox.Width,底部+外部空间);
groupBoxFlowLayout.Size=新尺寸(groupBoxFlowLayout.Width,底部);
}
});
Resize+=((s,e)=>resizeGroupBox());
groupBoxFlowLayout.ControlAdded+=((s,e)=>resizeGroupBox());
//使用不同数量的按钮填充每个流程面板
int numButtonsInGroupBox=3*(groupBoxIndex+1);
对于(int buttonIndex=0;buttonIndex
以下是调整为不同宽度的控件的三个屏幕截图:
是的,我在设计时在其中有控件。一旦我用已知数量的控件使一切正常工作。此代码将基于xml文件自动生成。在这一点上,我不知道会有多少控制。谢谢。这阻止了萎缩,但也造成了另一个问题。它水平增长,导致需要在GroupBox父容器中左右滚动。以前的滚动只是垂直滚动。您可以在FLP上设置MaximumSize.Width以避免这种情况。这很好,但是在调整groupbox的大小时,最好调整usercontrol本身的大小。