WinForm-SuspendLayout, ResumeLayout, and inclumlayou -- reprint, suspendresume

Source: Internet
Author: User

WinForm-SuspendLayout, ResumeLayout, and inclumlayou -- reprint, suspendresume

Problem

When we set attributes related to the appearance and Layout of the Child control, such as Size, Location, Anchor, or Dock, the Layout event of the Child control is triggered, the window may be re-painted. When many child controls are configured, if the preceding attributes are frequently set (for example, in the form initialization code), the Layout events of multiple child controls may cause window repainting efficiency problems, such as flashing. In particular, if there are too many UI objects generated by dynamically loading the plug-in, flashing is especially serious. So how can we solve this problem?

Solution

In this case, you can use the SuspendLayout method of the control to temporarily suspend the Layout of the control. The subsequent code will temporarily suspend the Layout event of the Child control, only setting the value of the corresponding attribute as a new value does not trigger the Layout event. After the ResumeLayout method is called, the Layout event of the Child control takes effect. When you need to execute a layout event immediately, you can directly call the layout mlayout method.

Q &

1. When will the Control. Layout event be triggered?
(1) when the Size of the control changes, the Layout event of the control is triggered.

(2) When the Location of its child control changes, its Layout event is triggered.

(3) adding or deleting a child control also causes its Layout event.

(4) Layout events that may affect the Layout of controls.
2. What is the use of the SuspendLayout method?
A Layout event occurs when a child control is added or removed, the control's border changes, and other changes that may affect the control Layout. You can use SuspendLayout to suspend the layout. You can perform multiple operations on the control without performing a layout operation for each change. That is to say, with this statement, the following operations are performed to add and delete a child control, or to change the size and position of the Child control and to change its own location, no Layout event is triggered.
3. What is the use of the ResumeLayout method?

You can use the ResumeLayout method to cancel the suspended layout. The Layout event will be triggered when the Layout changes in the future.
4. What is the PerformLayout method used?

The ResumeLayout method can cancel the suspended layout so that all subsequent la s are valid, but the layout cannot be executed immediately. To execute the Layout immediately (that is, to trigger the Layout event immediately), you can call the PerformLayout method to force the Layout and apply the Layout logic to itself and its child controls.

C # code generated by the Form Designer

/// <Summary>
/// The designer supports the required methods-do not
/// Use the code editor to modify the content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. panel1.SuspendLayout ();
This. SuspendLayout ();

... // Set the control property here

... // Set the control property here
This. panel1.ResumeLayout (false );
This. ResumeLayout (false );
}

From the code, we can see that when the Form Designer automatically generates code, it also changes the control attributes on a large scale (these attributes determine the control layout) you can use the merge mlayout and ResumeLayout methods of the control to improve the repainting efficiency and reduce the flicker.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.