Share via


The SuperSecretSideEffect of ResumeLayout(false)

Here's some really obscure layout information that probably you'll only need if you're doing visual inheritance or some fancy MDI goo.

One would imagine that this line of code:

panel.ResumeLayout()

Is equivilant to

panel.ResumeLayout(false)
panel.PerformLayout()

But not so... imagine I have a button placed in the bottom right hand corner of a panel:

button1.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
button1.Location = new Point(panel1.Width - button1.Width, panel1.Height-button1.Height);
panel1.Controls.Add(button1);

If I grow the panel by 10 pixels wrapped in a SuspendLayout() and ResumeLayout(), the panel is bigger by 10 pixels and the button is still in the corner.

panel1.SuspendLayout();
panel1.Height += 10;
panel1.ResumeLayout();

However if I use ResumeLayout(false), then PerformLayout(), the button will wind up 10 pixels from the bottom right edge of the panel.

panel1.SuspendLayout();
panel1.Height += 10;
panel1.ResumeLayout(false);
panel1.PerformLayout();

Whaa? 

There's a whole bunchof magic that goes on behind the scenes in order to calculate when to snap anchor distances. When you anchor something to the bottom | right edge, you're basically suggesting that you want to preserve the distance between you and some edge of your parent. The big question is when do we figure all of this information out? 

There's really one of two things that you want to have happen when you grow the dimensions of the panel. The typical case is that you want to preserve the distance from the edge - I was "x" pixels from the edge of the panel before, please make me "x" pixels from that edge now. For this, use ResumeLayout().

In a small number of cases, you want to totally reset. You may have totally re-arranged positions (typical in an inherited form), and you dont want to apply any of the calculations you previously made to the control. For this, use ResumeLayout(false), then PerformLayout().

I've talked a bit about Suspend/ResumeLayout before.