tags:

views:

71

answers:

1

I am reading up about SuspendLayout() and ResumeLayout(). What I can't figure out is, why should I do/use this. I know you use it when you add controls at runtime to a control-container.

It has something to do with setting properties like Dock, Anchor, Location, etc.

But I don't understand what the additional value is of Suspend- and ResumeLayout(). What does these methods take care for?

+3  A: 

Basically it's if you want to adjust multiple layout-related properties - or add multiple children - but avoid the layout system repeatedly reacting to your changes. You want it to only perform the layout at the very end, when everything's "ready".

Jon Skeet
I still don't understand what `SuspendLayout()` does. I mean why use `SuspendLayout()` You can also set these properties without using `SuspendLayout()`.
Martijn
@Martijn: Yes, you can set them... and the layout system will waste time (and possibly make the screen look ugly) changing the layout while you're setting the various properties - instead of doing it *once* when you've finished setting everything. Suppose you've got a complicated layout which tries to distribute space equally, and you're adding 100 items... do you *really* want it working out that space after you've added the first one, then after you've added the second, then after you've added the third etc?
Jon Skeet
As Jon Skeet wrote: When you set multipl properties (or insert multiple rows in a gridview) the screen is not refreshed for every single change, it is refreshed only once ResumeLayout is called. Saves performance and the odd screen-flicker.
Mario The Spoon
@Mario: So, you suggest that if multiple rows are inserted in a gridview, you should use `SuspendLayout()` to make sure that the rows are visible? Does the `BindingList` do this in the background too?
Martijn
@Martijn: I would use SuspendLayout(), but not to make sure they are visible, but to (a) save some performance since the screen is not refreshed after every action (b) to avoid unneccessary activity on the screen, like a flicker or so. How BindingList handles this, I do not know, sorry (since I do avoid automatic data bindings for other reasons).
Mario The Spoon