dotnetmonitor.com

 
Index
Previous
Next

 

Problem: Using the Resize event to size/position child controls circumvents the Suspend/Resume Layout perf protection.

 

Solution: Use the Layout event to size/position child controls.

 

private void button1_Click(object sender, EventArgs e) {

 

// Inefficient Layout practice

this.Resize += new System.EventHandler(this.Form1_Resize);

this.SuspendLayout();

this.Size = new Size(500, 500); // SuspendLayout circumvented!

this.ResumeLayout(false);

this.Resize -= new System.EventHandler(this.Form1_Resize);

 

// Efficent Layout pracitce

this.Layout += new LayoutEventHandler(Form1_Layout);

this.SuspendLayout();

this.Size = new Size(500, 500);

this.ResumeLayout(false);

this.Layout -= new LayoutEventHandler(Form1_Layout);

 

}

 

void Form1_Layout(object sender, LayoutEventArgs e) {

// for added performance, consider storing off the last size

// that happened when we got here and only perform the layout if the size has changed.

Rectangle bounds = this.ClientRectangle;

bounds.Inflate(-10, -10);

outerPanel.Bounds = bounds;

this.Text = "Resized in LAYOUT!";

}

 

private void Form1_Resize(object sender, EventArgs e) {

Rectangle bounds = this.ClientRectangle;

bounds.Inflate(-10, -10);

outerPanel.Bounds = bounds;

this.Text = "Resized on RESIZE!";

}

Terms
Docking Anchor Layout
What happens when I have Dock and Anchors that conflict?
Dock Scenarios
Padding Margin
You have a perf problem with Layout - how to diagnose?