Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Layout of a WinForms UserControl with both Static and Dynamic Content?

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?

like image 422
immitev Avatar asked Oct 28 '25 20:10

immitev


2 Answers

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.

like image 137
immitev Avatar answered Oct 30 '25 11:10

immitev


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.

like image 36
Joel Coehoorn Avatar answered Oct 30 '25 10:10

Joel Coehoorn