How can I get the panel's children to dynamically reset their Location.Y property?

I have a panel on a form, with the panel's AutoScroll property set to True.

On that panel I have placed several GroupBox controls, which take up more space than the panel does - thus, the panel sprouts a vertical scroll bar.

One of the panels can change height at runtime if need be (I programmatically set its height based on the amount of text it contains):

groupBox1.Height = label1.Height + label1.Top + 10;

This works fine if the panel increases in height - the GroupBoxes below it "move down." However, if the panel decreases in height, the GroupBoxes below do not "move up."

I know I can move the groupbox controls that are below upwards programmatically, but I'm thinking there must be a better/less fussy way of doing it than that...

Upvotes: 1

Views: 408

Answers (2)

Bill Tarbell
Bill Tarbell

Reputation: 5234

Set their docking property to Top. Then change the Z order of the children to specify their display order.

GroupBox1.Dock = DockStyle.Top;
GroupBox2.Dock = DockStyle.Top;
GroupBox3.Dock = DockStyle.Top;

If groupbox1 grows or shrinks the other 2 panels will move to accommodate.

Upvotes: 1

user1693593
user1693593

Reputation:

Replace the Panel with a FlowLayoutPanel instead and this will become automatic.

(you might want to turn off the wrapping of the FL-Panel)

Upvotes: 1

Related Questions