views:

1519

answers:

4

I have a user control that has: a) a buttons panel at the top (it always has to be visible) b) a panel with controls that are dynamically added and re-sized at run-time. The controls can be many, so the panel has to be scrollable.

This user control will be hosted in a form, with the following requirements: a) The initial size of the form will try to fit in maximum part of the dynamic content. b) On changing the form size, the control has to be re-sized accordingly.

I had played with various anchoring, docking, and auto-sizing and I don't quite get it working in the way I want to. Sometimes, it is the scrolling that messes up, sometimes it is something else.

What combination of anchoring, docking, and auto-sizing of the panels, usercontrol, form should work best to achieve the desired outcome?

A: 

Without knowing the specifics of your problem I find multiple fill docked split containers with one fixed panel and/or a fixed slider usually creates a really handy resizing experience. You can also collapse panels very effectively too.

Michael Prewecki
A: 

I'd go with a table layout panel. You can specify two rows by one column with the exact size for the buttons at the top and fill the rest with the bottom. Then put put either a normal panel or a flowlayoutpanel for the dynamic content in that area.

Joel Coehoorn
A: 

Hi Joel, Initially I went with table layout panel, but it has a nasty bug, so I resorted to using a normal panel, where I handle the child controls display and repositioning. For details about the bug, see: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98224

The AutoSize property on the TableLayoutPanel does not work. It would be nice to have the panel resize itself to fit the contents. For example, if all of my rows are set to "SizeType=AutoSize" and the panel is set to "AutoSize=true", the height of the panel does not fit the contents. Instead, the last row is stretched to fit the table's size itself.

immitev
+1  A: 

I succeeded to meet the requirements. Here is my solution:

The dynamic panel is anchored to the top and the bottom of the control. It does not AutoSize, it manually changes its MaximumSize and PreferredSize after change in the contents.

The form hosts the form using:

        cntrl.AutoSize = true;
        cntrl.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
        cntrl.Dock = System.Windows.Forms.DockStyle.Fill;

The form subscribes to a custom control's event that notifies for the preferredHeight and it changes its own Height accordingly.

immitev