12

我有一个包含一个流程布局面板的组框,流程布局面板包含一堆控件。我将 flowlayout 面板设置为与父级对接。由于我不知道面板中有多少控件,所以我将分组框 autosize 设置为 true 并将 autosizemode 设置为增大和缩小。当我这样做时,组框会缩小,就好像它是空的一样。我需要标题,所以我不能删除组框。有谁知道为什么会这样?

4

4 回答 4

7

没有什么可以阻止 FlowLayoutPanel 缩小为空。您至少还必须将其 AutoSize 属性设置为 True。

于 2010-07-27T17:14:21.423 回答
5

我今天试图做同样的事情。下面是我想出的解决方案,就是将 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);
            }

        }


    }

}

以下是调整为各种不同宽度的控件的三个屏幕截图:

控件的三个屏幕截图调整为各种不同的宽度

于 2013-01-29T18:55:19.090 回答
0

您说“我不知道面板中有多少控件”。在设计时 FlowLayoutPanel 中有任何控件吗?如果你不这样做,这听起来像是预期的行为。Panel 没有任何内容,因此其所需大小为零,因此 GroupBox 的所需大小为零。

如果是这种情况,那么当您在运行时实际添加控件时,它应该会全部变大。

于 2010-07-27T17:13:14.150 回答
0

您为 groupBox 设置属性 Anchor:Top、Bottom、Left、Right。

于 2012-06-28T12:15:18.523 回答