views:

608

answers:

2

A while back, I posted this question about trying to get a flowlayoutpanel to autosize properly when docked to the top of a container. What I was trying accomplish was to setup a horizontal (left-to-right) flowlayoutpanel spanning across the top edge of a form, and a datagridview taking up the rest of the available space below the flowlayoutpanel. I was having trouble getting the flowlayoutpanel's height property to autosize the way it should. The posted answer worked perfectly for what I was trying to accomplish, which was great...

Unable to leave well enough alone, I am now trying to go one step further and wrap the flowlayoutpanel in a UserControl. I've discovered, however, that doing this causes the flowlayoutpanel to no longer resize its height properly.

Here's the first layout, which works beautifully:

FlowLayoutPanel1 (Autosize = true, Dock = Top)
  ComboBox1
  ComboBox2
  ComboBox3
DataGridView1 (Dock = Fill)

And here it is with flowlayoutpanel wrapped in a UserControl:

UserControl (Autosize = true, Dock = Top)
  FlowLayoutPanel1 (Autosizse = true, Dock = Fill)
    ComboBox1
    ComboBox2
    ComboBox3
DataGridView1 (Dock = Fill)

For whatever reason, flowlayoutpanel doesn't resize its height properly when the form is resized. This is pretty easy to reproduce. Oh also, the FlowLayoutPanel's contents can be anything, not just ComboBoxes.

A: 

Does your UserControl have a width set? Look in the designer.cs to be sure. If it does, you'll need to reset that property.

Rob Fonseca-Ensor
The width seems to be working OK, but the height remains constant. When the flowlayoutpanel's children wrap down to form new lines, the height does not increase accordingly and the children are clipped vertically.
echo
+1  A: 

I think what the problem is is that the you have FlowLayoutPanel1.Dock = Fill which overrides it's attempt to autosize it's height.

The fix is probably to set FlowLayoutPanel1.Dock = Top (as you had it before), and then handle the FlowLayoutPanel1.Resize event to resize your user control. Essentially, you're manually trying to keep the UserControl.Size in sync with the FlowLayoutPanel1.Size.

There could be a smarter way to do that, but hopefully it will get you on the right path...

Lenny
This almost works. The Resize event works perfectly, but I have to resize the form to get the Resize event to fire. I'd like this size adjustment to occur when the form is initially shown, not just after it has been resized. I've tried the UserControl.Load, Layout, VisibleChanged, and Paint events with no success.
echo
I'm noticing that there's a FlowLayoutPanel.SizeChanged event, maybe give that i try? I'm not near a machine with VS set up so I can't play around with it myself. You could also try the FlowLayoutPanel.ControlAdded event
Lenny
SizeChanged did the trick. Thanks
echo