C# SuspendLayout()和ResumeLayout()

Temporarily suspends the layout logic for the control.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
public void SuspendLayout()

The layout logic of the control is suspended until the ResumeLayout method is called.

The SuspendLayout and ResumeLayout methods are used in tandem to suppress multiple Layout events while you adjust multiple attributes of the control. For example, you would typically call the SuspendLayout method, then set the SizeLocationAnchor, or Dock properties of the control, and then call the ResumeLayout method to enable the changes to take effect.

There must be no pending calls to SuspendLayout for ResumeLayout to be successfully called.

NoteNote

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.

The following code example adds two buttons to a form. The example transactions the addition of the buttons by using the SuspendLayout and ResumeLayout methods.

private void AddButtons()
{
   // Suspend the form layout and add two buttons. 
   this.SuspendLayout();
   Button buttonOK = new Button();
   buttonOK.Location = new Point(10, 10);
   buttonOK.Size = new Size(75, 25);
   buttonOK.Text = "OK";

   Button buttonCancel = new Button();
   buttonCancel.Location = new Point(90, 10);
   buttonCancel.Size = new Size(75, 25);
   buttonCancel.Text = "Cancel";

   this.Controls.AddRange(new Control[]{buttonOK, buttonCancel});
   this.ResumeLayout();
}
阅读更多

关于SuspendLayout方法

10-23

据说是可以挂起,然后ResumeLayout()结束挂起。rn[code=C#]rnusing System;rnusing System.Collections.Generic;rnusing System.ComponentModel;rnusing System.Data;rnusing System.Drawing;rnusing System.Text;rnusing System.Windows.Forms;rnrnnamespace WindowsApplication1rnrn public partial class Form1 : Formrn rn private string path = "../../QQ截图未命名.png";rn public Form1()rn rn InitializeComponent();rn rnrn private void Form1_Load(object sender, EventArgs e)rn rn TJ();rn rn private void TJ()rn rn NewMethod();rn //this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 40F));rn rnrn private void NewMethod()rn rn this.tableLayoutPanel1.SuspendLayout();rn this.tableLayoutPanel1.Controls.Clear();rn this.tableLayoutPanel1.RowCount = 0;rn string[] strs = new string[] "1", "2", "1", "2", "../../QQ截图未命名.png", "2", "1", "2" ;rn string[] strs1 = new string[] "1", "2", "1", "2", "1", "../../QQ截图未命名.png", "1", "2" ;rn string[] strs2 = new string[] "1", "2", "../../QQ截图未命名.png", "2", "1", "2", "1", "2" ;rn string[] strs3 = new string[] "1", "2", "1", "2", "../../QQ截图未命名.png", "2", "1", "2" ;rn this.tableLayoutPanel1.ColumnCount = 1;rn List list = new List();rn list.Add(strs);rn list.Add(strs1);rn list.Add(strs2);rn list.Add(strs3);rn foreach (string[] strss in list)rn rn this.tableLayoutPanel1.RowCount += 1;rn for (int i = 0; i < strs.Length; i++)rn rn if (this.tableLayoutPanel1.ColumnCount < strs.Length)rn rn this.tableLayoutPanel1.ColumnCount += 1;rn //this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));rn rn PictureBox p = new PictureBox();rn if (strss[i] == path)rn rn p.Image = Image.FromFile(path);rn rn this.tableLayoutPanel1.Controls.Add(p);rn rn rn this.tableLayoutPanel1.ResumeLayout(); rn rnrn private void button1_Click(object sender, EventArgs e)rn rn this.tableLayoutPanel1.SuspendLayout();rn this.tableLayoutPanel1.Controls.Clear();rn rn this.tableLayoutPanel1.RowCount = 0;rn string[] strs = new string[] "1", "2", "1", "2", "../../QQ截图未命名.png", "2", "1", "2" ;rn string[] strs1 = new string[] "1", "2", "1", "2", "1", "../../QQ截图未命名.png", "1", "2" ;rn string[] strs2 = new string[] "1", "2", "../../QQ截图未命名.png", "2", "1", "2", "1", "2" ;rn string[] strs3 = new string[] "1", "2", "1", "2", "../../QQ截图未命名.png", "2", "1", "2" ;rnrn this.tableLayoutPanel1.ColumnCount = 1;rn List list = new List();rn list.Add(strs1);rn list.Add(strs2);rn list.Add(strs2);rn list.Add(strs3);rn foreach (string[] strss in list)rn rn this.tableLayoutPanel1.RowCount += 1;rn for (int i = 0; i < strs.Length; i++)rn rn if (this.tableLayoutPanel1.ColumnCount < strs.Length)rn rn this.tableLayoutPanel1.ColumnCount += 1;rn rn rn if (strss[i] == path)rn rn PictureBox p = new PictureBox();rn p.Image = Image.FromFile(path);rn this.tableLayoutPanel1.Controls.Add(p);rn this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));rn rn rn rn rn this.ResumeLayout();rn rnrn private void button2_Click(object sender, EventArgs e)rn rn NewMethod();rn rnrn rn rnrnrn加了不加这两个方法可以出来4个图,加了之后,就出来了一个图。。。。不知道为什么。rnrn还有呀,其实加这个方法为了解决NEW了这么多控件,会出现闪烁的问题,有没有别的方法呢?rnrn[/code]

没有更多推荐了,返回首页