c# - FlowLayoutPanel 中奇怪的空格

标签 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);

        }
    }
}

还有几张图片来说明我的意思:

Image1: Strange space under the Label enter image description here

Image2: No space under the Label when the form is resized (this is how I'd like this to work) enter image description here

最佳答案

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

It is a bug, same one as this one. The extra space is the height of the next label. The workaround is exactly the same, just add a dummy control with a Width of 0 after the label. – Hans Passant

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

flowLayoutPanel1.SetFlowBreak(label, true);

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

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);

Fixed

关于c# - FlowLayoutPanel 中奇怪的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23448229/

相关文章:

c# - 如何在Application_Error()中知道请求是否在asp.net中是ajax

c# - 接受两个接口(interface)之一的方法

c# - 什么是 C# 中的 Delphi "shl"?

c# - 将控件添加到 FlowLayoutPanel 时意外的索引更改

c# - FlowLayoutPanel 显示水平滚动条,内部​​面板的宽度与客户端尺寸宽度相同

c# - 为什么将值转换为 IEnumerable<T> 的行为会根据我初始化值的方式而有所不同?

c# - Microsoft Sam,SAPI 替代品

c# - Winforms:带对接的 FlowLayoutPanel

c# - FlowLayoutPanel 自动调整大小

c# - Winforms FlowLayoutPanel 使用自动调整控件删除不需要的空间