我在 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
Image2: No space under the Label when the form is resized (this is how I'd like this to work)
最佳答案
谢谢汉斯!我认为这是一个真正的答案,因为它解决了我的问题:(引自评论)
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);
关于c# - FlowLayoutPanel 中奇怪的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23448229/