开发者

WinForms UserControl Layout Autosize Problems

A while back, I posted this ques开发者_如何学编程tion 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.


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...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜