Nested flowlayout panel not wrapping
I've got a FlowLayoutPanel with properties:
- Dock = Fill (in a usercontrol)
- FlowDirection = TopDown
- WrapContents = false
I do it this way so that each item added to the panel gets added to the bottom.
The items that I add to this panel are usercontrols which themselves have FlowLayoutPanels on them, however they have the standard behaviour (LeftToRight, WrapContents = true). The problem that I'm having is that the interior usercontrol's FlowLayoutPanel isn't resizing to fill the outer control, but when I set autosizing to true on these controls, then the panel won't wrap its contents - which is a known problem apparently.
开发者_JAVA技巧If it helps visualize what I'm trying to do, it looks like this:
______________________________ | __________________________ | Outer box = exterior flowlayout | |Text____________________| | (TopDown, NoWrap) | | # # # # # # # # # # # #| | | | # # # # | | Interior boxes = usercontrols with text and a | |________________________| | flowlayoutpanel on them | __________________________ | (LeftToRight, Wrap) | |Text____________________| | | | # # # # # # # # # # # #| | # = pictures | | # # | | | |________________________| | |____________________________|
I don't think you can dock controls in a FlowLayoutPanel, unless you subclass LayoutEngine and make your own version of the pane using your custom engine. However, there's an awesome solution to this problem. Use a TableLayoutPanel! Since you only want 1 column, it's very easy to use a TableLayoutPanel for this purpose.
The only caveat is that the TLP needs to have 0 rows initially, and you then add the user controls programmatically. And the trick is to dock the user control to Top. This works:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TableLayoutPanel tlp1 = new TableLayoutPanel();
this.Controls.Add(tlp1);
tlp1.Dock = DockStyle.Fill;
for (int i = 0; i < 5; i++)
{
UserControl1 uc = new UserControl1();
uc.Dock = DockStyle.Top;
tlp1.Controls.Add(uc);
}
}
}
UserControl1 in this case was a user control with a FLP on it which had a bunch of buttons in it so I could confirm that the docking and flowing would work.
精彩评论