C#: Does ResumeLayout(true) do the same as ResumeLayout(false) + PerformLayout()?

寵の児 提交于 2020-05-12 11:34:30

问题


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...


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/1339758/c-does-resumelayouttrue-do-the-same-as-resumelayoutfalse-performlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!