17

I have looked at the generated designer code of Forms and UserControls, and in the InitializeComponent() method they always start with

    this.SuspendLayout();

and end with

    this.ResumeLayout(false);
    this.PerformLayout();

But from what I can see in the msdn documentation of those methods, wouldn't ending with

    this.ResumeLayout(true); // Or just this.ResumeLayout()

do the exact same thing? Or am I missing something here?

Asking because I will be adding a bunch of controls in a different method, and thought I should do the suspend-resume routine to be nice and efficient. But can't figure out what the reason for those two method calls are when you can seemingly just use one...

Kara
  • 6,115
  • 16
  • 50
  • 57
Svish
  • 152,914
  • 173
  • 462
  • 620

2 Answers2

8

Using reflector:

this.ResumeLayout() is equal to this.ResumeLayout(true)

But

this.ResumeLayout(true) is not equal to this.ResumeLayout(false) + this.PerformLayout()

Reason:
When ResumeLayout is called with false, there is a control collection that is looped through and the LayoutEngine calls InitLayout on each of the controls in the layout.

SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
  • So to get the correct behaviour, I guess I would have to call them both? – Svish Aug 27 '09 at 12:25
  • Unfortunately it looks that way – SwDevMan81 Aug 27 '09 at 12:30
  • 1
    Alright. Well, thanks for the info then :) I must say that I don't quite understand the point of `ResumeLayout(true)` then, but it probably has its reason. – Svish Aug 27 '09 at 13:04
  • I'd like to request somebody volunteer a common or typical scenario where a "true" parameter will produce different behavior or results than a "false" parameter. Thanks. – FloverOwe Sep 07 '22 at 21:05
4

SuspendLayout

When adding several controls to a parent control, it is recommended that you call the SuspendLayout method before initializing the controls to be added. After adding the controls to the parent control, call the ResumeLayout method. This will increase the performance of applications with many controls.

PerformLayout

It forces the control to apply layout logic to all its child controls. If the SuspendLayout method was called before calling the PerformLayout method, the Layout event is suppressed. The layout event can be suppressed using the SuspendLayout and ResumeLayout methods.

MSDN Link - PerformLayout Method

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • That makes sense, but what about `ResumeLayout`? Does calling `ResumeLayout(true)` perform the same work as calling `ResumeLayout(false)` and `PerformLayout()`? – Svish Aug 27 '09 at 09:34
  • 2
    I think this link will answer your question - http://www.ddj.com/architect/184405892 – KV Prajapati Aug 27 '09 at 10:07