问题
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