简体   繁体   中英

(C#) Does SuspendLayout cascade to child controls?

C#: Does SuspendLayout cascade to child controls?

Do I have to iterate the child of the control myself to call suspendlayout on them? and on their grand child? grand grand child?? grand grand grand child?

Thanks

SuspendLayout doesn't propagate recursively, but depending on what you are doing, suspending just specific controls may be enough.

The rule of a thumb is: you suspend the control whose layout you are about to mess with more than once.

For instance:

  • If a control contains a number of children, and you intend to add one, you don't need to suspend anything.
  • If you intend to add more than one child to the parent, you should suspend the parent.
  • If you intend to change the Size property of one child, no need to suspend anything. The child will be changed only once, and that will, in turn, cause only one change in the parent.
  • If you intend to change the Size of multiple children, you should only suspend the parent. Children only get one resize each, so no need to suspend them. But each one of those children would cause the parent to relayout, so suspend the parent to do all those relayouts in bulk once you call ResumeLayout .
  • If you intend to change, for instance Size , Location and Bounds properties on multiple children, you should suspend both the parent and the children. All of them will have multiple layout events triggering. Just be sure to only resume the parent after all its children have resumed. Otherwise, each of the children's ResumeLayout s would cause a relayout of the parent so you would get no benefit from suspending it in the first place.
  • If a parent has multiple children, but you only wish to change the Size of the parent, no need to suspend anything. This will indirectly modify a bunch of children, but the parent is the one doing their modifications so it should already know how to handle that efficiently. It already does it every time the user resizes the window, for instance.
  • If more than one of the parent's layout-related properties are getting changed, but none of the children's, only the parent should be suspended. The reasoning is the same as for the previous point, only suspending the parent so it itself only produces one layout event.

Yes; SuspendLayout stops the control being painted (or rather layout requests being processed), which by default trickles down the child structure.

In other words: there is "no-one" calling the paint routine anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM