问题
I have a FlowLayoutPanel and a UserControl.
I've added multiple usercontrols into the FlowLayoutPanel
, and I'm trying to dock them to the top, so when I change the size of the FlowLayoutPanel
the size (width) of the usercontrols changes accordingly.
回答1:
You cannot dock anything inside a FlowLayoutPanel, it's simply ignored.
Check out the answer here apparently posted by the Microsoft team.
They say:
The FlowLayoutPanel relies on a largest control to effectively define the column/row within it. The code below set's the size of the first control to the width of the FLP to achieve a layout similar to what you want.
private void flowLayoutPanel1_Layout(object sender, LayoutEventArgs e)
{
flowLayoutPanel1.Controls[0].Dock = DockStyle.None;
for (int i = 1; i < flowLayoutPanel1.Controls.Count; i++)
{
flowLayoutPanel1.Controls[i].Dock = DockStyle.Top;
}
flowLayoutPanel1.Controls[0].Width = flowLayoutPanel1.DisplayRectangle.Width - flowLayoutPanel1.Controls[0].Margin.Horizontal;
}
Key thing is to use the Layout
event.
This solution worked for me up to a point. Your UserControls have to have AutoSize
turned off / stay a uniform size.
In my case I wanted AutoSize
turned on so as to allow the UserControl to expand/contract vertically while filling the width of the FlowLayoutPanel.
I had to find a different solution. But the above might help you in your case.
来源:https://stackoverflow.com/questions/7880593/how-do-i-dock-a-usercontrol-into-a-flowlayoutpanel