18

I am developing a Windows Forms application (.NET 2.0, VS 2005). I have a form that essentially contains a panel that is dynamically resized with the form :

this.panel1.Dock=DockStyle.Fill;

This panel is simply used as a container. At runtime, a custom control will be added :

UserControl uc=new UserControl();
panel1.Controls.Add(uc);
uc.Dock=DockStyle.Fill;

As this custom control has a minimum size requirement, I want scroll bars to appear on the containing panel if it gets too small to show the entire control :

this.panel1.AutoScroll=true;

This does not work. I have tried to resize the panel using the Anchor property rather than the Dock property, to no avail.

Mac
  • 8,191
  • 4
  • 40
  • 51

3 Answers3

28

Don't Dock your User control. Use the Anchor property instead. (Anchor to all four sides). Set you User control's MinimumSize property Set the panel's AutoScrollMinSize property

The Panel's scrollbars won't appear unless you set its AutoScrollMinSize property. Setting the user control's Dock to Fill seems to hide the panel's scrollbars.

NascarEd
  • 1,380
  • 12
  • 14
  • 12
    The Dock property is fine for both the panel and the user control, as long as AutoScrollMinSize is set (http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/56267eb1-25af-4df2-82a3-0498563c7eef). – Mac Aug 04 '09 at 15:16
  • Anchoring is not the same as Docking though. For example if I make a docked control invisible then it automatically rearranges the following controls. Anchoring does not give you this ability. Docking is very useful but like the OP says it does not always work as you'd hope with a parent Autoscroll control - even when setting the minimum size of the docked control – Gwynge Mar 22 '17 at 11:45
2

After InitializeComponent in the dialog form, I did this and it seemed to work:

tableLayoutPanel1.AutoScrollMinSize = tableLayoutPanel1.GetPreferredSize(new Size(1, 1));

I have a bunch of subpanels in the table. All the rows and columns are set to AutoSize. For some reason, the control is not smart enough tell the scroll control it's preferred size.

Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
1

Panel.AutoScroll= *True works - NOW! I played and played with the ideas above, for both the panel and user control, and finally reset everything back to default, set Autoscroll on panel, and... voila! somehow it works again...go figure...