查找 FlowLayoutPanel 的行和列

发布于 2024-10-30 19:30:10 字数 61 浏览 1 评论 0原文

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

Just wondering if there is a way to easily lookup the rows and columns currently in a FlowLayoutPanel or if a manual calculation is required?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不喜欢何必死缠烂打 2024-11-06 19:30:10

该线程很旧,但我今天才收到要求,并且 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!

CT

This thread is old yet I had the requirement just today and GetFlowBreak was failing to return true on a control that caused the flow panel to break onto a new line. I am not sure why and I didn't have time to figure it out. This works for FlowDirection = LeftToRight.

Frankly, I don't have time to write this but I am anyway. Here is a simple extension method that will calculate the number of rows:

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

Use:

    int rows = ChoiceFlow.GetRowCount();

HTH!

CT

请止步禁区 2024-11-06 19:30:10

上面的例子对我来说不起作用。它从未根据 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;
    }

在相同控件的情况下,您不需要使用此方法迭代控件

The above example was not working for me. It never gave the correct number of rows depending on the FlowLayoutPanel size..

So I updated the solution above. Assuming, that all controls in the FlowLayoutPanel have same size:

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

In the case of same controls, you don't need to iterate through the controls with this method

幸福丶如此 2024-11-06 19:30:10

我找到了一个非常简单的方法来确定这一点。您必须考虑边距和填充。我只是碰巧使用了 2 的边距和 0 的填充,所以我使用了较小的左侧位置值。 X 位置具有较小值的任何控件都将位于流程布局面板的左侧旁边。如果您想计算顶部控件的数量,只需检查 Y 位置值即可。

public int GetRowCount(FlowLayoutPanel flowPanel)
{
    int rows = 0;

    foreach (Control cntrl in flowPanel.Controls)
    {
        if (cntrl.Location.X < 2) // Allow for a small margin or padding
        {
            rows += 1;
        }
    }

    return rows;
}

I found a very simple way to determine this. You have to account for margins and padding. I just happened to be using a margin of 2 and padding of 0, so I used a smaller left location value. Any controls with that small value for the X location will be next to the left of the flowlayout panel. If you wanted to count the number of controls at the top just check the Y location value instead.

public int GetRowCount(FlowLayoutPanel flowPanel)
{
    int rows = 0;

    foreach (Control cntrl in flowPanel.Controls)
    {
        if (cntrl.Location.X < 2) // Allow for a small margin or padding
        {
            rows += 1;
        }
    }

    return rows;
}
梦巷 2024-11-06 19:30:10

下面是一个使用 linq 计算高度的示例:

var heightNeeded = flowLayoutPanel1.Controls.OfType<Control>()
    .Max(x => x.Location.Y + x.Height) + 7;

Here is an example using linq to calculate the height:

var heightNeeded = flowLayoutPanel1.Controls.OfType<Control>()
    .Max(x => x.Location.Y + x.Height) + 7;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文