Find the rows and columns of a FlowLayoutPanel

情到浓时终转凉″ 提交于 2019-12-13 00:19:42

问题


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?


回答1:


A manual calculation is required.




回答2:


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




回答3:


Here is an example using linq to calculate the height:

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


来源:https://stackoverflow.com/questions/5561954/find-the-rows-and-columns-of-a-flowlayoutpanel

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