Strange empty spaces in FlowLayoutPanel

限于喜欢 提交于 2019-12-19 19:01:11

问题


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?

Code:

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:

Image1: Strange space under the Label

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


回答1:


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

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

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



来源:https://stackoverflow.com/questions/23448229/strange-empty-spaces-in-flowlayoutpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!