1

只是想知道是否有一种方法可以轻松地查找 FlowLayoutPanel 中当前的行和列,或者是否需要手动计算?

4

3 回答 3

0

上面的例子对我不起作用。它从来没有根据FlowLayoutPanel大小给出正确的行数..

所以我更新了上面的解决方案。假设所有控件FlowLayoutPanel都具有相同的大小:

    FlowLayoutPanel flp = new FlowLayoutPanel();
    private int GetRowCount()
    {
        int rows;

        int Col = (flp.ClientRectangle.Width - flp.Padding.Left - flp.Padding.Right) / (flp.Controls[0].Width  + flp.Controls[0].Margin.Left + flp.Controls[0].Margin.Right);
        rows = flp.Controls.Count / Col;
        if (flp.Controls.Count % Col != 0)
            rows += 1;
        return rows;
    }

    private int GetFlowHeight()
    {
        if (flp.Controls.Count == 0)
            return 0;

        int h = ((flp.Controls[0].Height + flp.Controls[0].Margin.Top + flp.Controls[0].Margin.Bottom) * GetRowCount()) + flp.Padding.Top + flp.Padding.Bottom;
        return h;
    }

在相同控件的情况下,您无需使用此方法遍历控件

于 2021-09-04T17:18:49.793 回答
0

这个线程很旧,但我今天有需求,GetFlowBreak 未能在导致流面板中断到新行的控件上返回 true。我不知道为什么,我也没有时间弄清楚。这适用于 FlowDirection = LeftToRight。

坦率地说,我没有时间写这个,但无论如何我都是。这是一个简单的扩展方法,它将计算行数:

    public static int GetRowCount(this FlowLayoutPanel flowPanel)
    {
        int rows = 1;

        int rowWidth = flowPanel.ClientRectangle.Width;

        foreach (Control control in flowPanel.Controls)
        {
            rowWidth -= control.Width;

            if (rowWidth > 0)
            {
                continue;
            }

            rows += 1;
            rowWidth = flowPanel.ClientRectangle.Width;
        }

        return rows;
    }

利用:

    int rows = ChoiceFlow.GetRowCount();

HTH!

计算机断层扫描

于 2019-11-12T03:05:53.250 回答
-2

这是使用 linq 计算高度的示例:

var heightNeeded = flowLayoutPanel1.Controls.OfType<Control>()
    .Max(x => x.Location.Y + x.Height) + 7;
于 2014-10-24T20:44:19.703 回答