FlowLayoutPanel中奇怪的空白空间 [英] Strange empty spaces in FlowLayoutPanel

查看:191
本文介绍了FlowLayoutPanel中奇怪的空白空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在flowlayoutpanel上有很多按钮,然后有文本标签可以中断流程.标签前面的最后一个按钮和标签本身具有SetFlowBreak.一切工作都很好,但是我不明白,为什么文本标签下有这么多空间?如果将表单的大小调整得如此狭窄以至于只有一列按钮,那么多余的空间就会消失.有人可以解释如何删除该空间吗?

I have lots of buttons on flowlayoutpanel, and then there's text labels to break the flow. Last button before label and label itself has SetFlowBreak. All works kind of fine, but what I don't understand, is why there is so much space under the text label? If form is resized so narrow that there's only one column of buttons, then the unwanted space disappears. Can someone explain how that space can be removed?

代码:

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

        }
    }
}

还有几张图片展示了我的意思:

And couple of images to show what I mean:

图像1:标签下的奇怪空间

图片2:调整表单大小时,标签下没有空格(这是我的方式想让它工作)

推荐答案

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

Thank you Hans! I thinks this is a real answer, as it solved my problem: (quote from comments)

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

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:

So first I removed flowbreak after the real label:

flowLayoutPanel1.SetFlowBreak(label, true);

然后用下面的代码替换它,神秘的空间消失了!

And then replaced it with the following code, and the mysterious space disappeared!

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

这篇关于FlowLayoutPanel中奇怪的空白空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆