【问题标题】:Groupbox with a flowlayout panel inside and autosize = true shrinks like it is empty内部带有 flowlayout 面板和 autosize = true 的 Groupbox 像空一样收缩
【发布时间】:2013-01-30 02:55:19
【问题描述】:

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

【问题讨论】:

    标签: c# autosize groupbox flowlayoutpanel


    【解决方案1】:

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

    【讨论】:

    • 谢谢。这停止了​​收缩,但引起了另一个问题。它水平增长,导致需要在 groupboxes 父容器中左右滚动。之前的滚动只是垂直的。
    • 您可以在 FLP 上设置 MaximumSize.Width 来避免这种情况。
    【解决方案2】:

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

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

    【讨论】:

    • 这很好用,但是在调整组框大小时调整用户控件本身的大小是个好主意。
    【解决方案3】:

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

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

    【讨论】:

    • 是的,我在设计时有控件。一旦我使用已知数量的控件完成所有工作。此代码将基于 xml 文件自动生成。到那时我不知道会有多少控件。
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多