Friday, August 21, 2009

SuspendLayout and ResumeLayou

SuspendLayout and ResumeLayout

The SuspendLayout and ResumeLayout are methods used in order to prevent conflict when events pertaining to placing and moving controls and setting their attributes are thrown.

You would for instance perhaps like to add controls onto a panel or a window programatically .. what you must do is suspend the control you are adding other controls to and after you are done adding ,you then can resume your layout by calling the ResumeLayout method.

say for example you have a panel and would like to add some controls to it here is how you would do it using a method :

private void AddSomeControls()
{
// Suspend the panel layout and add two buttons.
this.SuspendLayout();
TextBox txtOne = new TextBox();
txtOne.Location = new Point(10, 10);
txtOne.Size = new Size(75, 25);
txtOne.Text = "Textbox addded";

Button btnMyButton = new Button();
btnMyButton.Location = new Point(90, 10);
btnMyButton.Size = new Size(75, 25);
btnMyButton.Text = "Just been Added";

this.Controls.AddRange(new Control[]{txtOne, btnMyButton});
this.ResumeLayout();
}