In response to this situation, the Windows Forms Designer employs a special optimization that relies on two methods: SuspendLayout and ResumeLayout. Calls to these methods are placed in InitializeComponent for any form that has at least one control:
// ContainmentForm.cs
partial class ContainmentForm {
...
void InitializeComponent() {
// Hosted control and component instantiation
...
this.SuspendLayout();
// Hosted control, component, and form initialization
...
// Controls added to form
...
this.ResumeLayout(true);
...
}
}By bracketing several tasksthe child control creation and initialization as well as the addition of the controls to the control collectionin SuspendLayout and ResumeLayout, we prevent the form from trying to draw itself until everything is set up. However, SuspendLayout and ResumeLayout operate only one level deep, so if your form hosts controls within container controls like Panel, you also need to call SuspendLayout and ResumeLayout on the panel. You can do this if you need to make a nontrivial set of changes to the form's properties or controls yourself, a situation you'll encounter when the layout system can't do quite what you need.