FlowLayoutPanel中的奇怪空格

dar*_*arx 6 c# flowlayoutpanel

我在flowlayoutpanel上有很多按钮,然后有文本标签来打破流程.标签和标签本身之前的最后一个按钮SetFlowBreak.一切都很好,但我不明白,为什么文本标签下面有这么多空间?如果窗体的大小调整得如此之窄,以至于只有一列按钮,那么不需要的空间就会消失.有人可以解释如何删除该空间?

码:

public Form1()
{
    InitializeComponent();

    for (int i = 1; i <= 100; i++)
    {
        Button button = new Button();
        button.Text = i.ToString();
        button.Width = 150;
        button.Height = 50;
        button.Margin = new Padding(5);
        flowLayoutPanel1.Controls.Add(button);

        if (i % 10 == 0)
        {
            flowLayoutPanel1.SetFlowBreak(button, true);

            Label label = new Label();
            label.Text = "Some random text";
            label.AutoSize = true;
            label.Margin = new Padding(5, 5, 0, 0);
            label.BackColor = ColorTranslator.FromHtml("#ccc");
            flowLayoutPanel1.Controls.Add(label);

            flowLayoutPanel1.SetFlowBreak(label, true);

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

和几个图像来表明我的意思:

Image1:标签下的奇怪空间 在此输入图像描述

Image2:调整表单大小时,Label下没有空格(这就是我喜欢这个的方式) 在此输入图像描述

dar*_*arx 5

谢谢汉斯!我认为这是一个真正的答案,因为它解决了我的问题:(引用自评论)

这是一个错误,与相同。额外的空间是下一个标签的高度。解决方法完全一样,只是在标签后添加一个宽度为0的虚拟控件。— 汉斯·帕桑特

所以首先我在真正的标签之后删除了 flowbreak:

flowLayoutPanel1.SetFlowBreak(label, true);
Run Code Online (Sandbox Code Playgroud)

然后换成下面的代码,神秘空间就消失了!

Label dummyLabel = new Label();
dummyLabel.Width = 0;
dummyLabel.Height = 0;
dummyLabel.Margin = new Padding(0, 0, 0, 0);

flowLayoutPanel1.Controls.Add(dummyLabel);
flowLayoutPanel1.SetFlowBreak(dummyLabel, true);
Run Code Online (Sandbox Code Playgroud)

固定的