Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize panel to fit contained elements in windows forms

I am creating a collapsible panel element, which would essentially be a panel element with a button element and a panel element below the button. Clicking the button causes the neighboring panel to have Visible = false. I would like to resize the containing panel when the child panel is set to invisible.

I have done this manually, by setting the Size property to be the sum of the widths and heights of the visible elements (either the button or the button and the child panel.)

I am curious to know though if there was a way to force the resize of the containing panel without manually calling Size.

I guess I'm looking for the inverse of the property Dock=Fill, which automatically resizes elements based on the size of their containing element.

Thanks in advance.

like image 239
user420667 Avatar asked Sep 27 '11 20:09

user420667


People also ask

How do I make my windows form resizable?

To make a control resize with the form, set the Anchor to all four sides, or set Dock to Fill . It's fairly intuitive once you start using it. For example, if you have OK/Cancel buttons in the lower right of your dialog, set the Anchor property to Bottom and Right to have them drag properly on the form.

How do I fit windows form to any resolution?

simply set Autoscroll = true for ur windows form..

How do I fix windows form size?

Right click on your form and go to properties. Then go to Layout options,see there are a property named Size. Change it as your need as width and length wise. Also see a property named StartPosition just after the Size property.

Is it possible to resize a control within the form design window?

To resize multiple controls on a form In Visual Studio, hold down the Ctrl or Shift key and select the controls you want to resize. The size of the first control you select is used for the other controls.


1 Answers

How about doing:

panel1.Size = new Size(0, 0);
panel1.AutoSize = true;

and then instead of changing the visibility, do this:

panel1.Controls.Remove(panel2);

and when you want to bring it back:

panel1.Controls.Add(panel2);

(panel1 is the back-panel)

like image 108
ispiro Avatar answered Sep 19 '22 14:09

ispiro