
Nguyên bản được gửi bởi
nttt
Ai có thể cho mình biết ý nghĩa của hàm SuspendLayout() trong Form. Cảm ơn nhiều.
Phương thức này dùng để tránh sự xung đột trong quá trình thêm Form trong lúc Run-time, chẳng hạn khi bạn thêm 2 button, chúng vô tình có cùng tọa độ, tức là có cùng 1 layer (lớp trên form), thật ra đối với newbie tụi mình thì đó không phải lỗi ,và sự thực compiler vẫn chấp nhận, nhưng những dân lập trình chuyên nghiệp thì họ coi đây là 1 lỗi và bác Bill nhà ta đã "chế" ra phương thức trên cho form để khắc phục hiện tượng trên.
Trong MSDN, có ghi vài dòng sau:
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.
Sau đây là 1 ví dụ lấy từ đó:
Code:
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();
}
Ngoài ra, bạn có thể tham khảo thêm ở link sau:
http://www.andreavb.com/forum/viewtopic_1937.html