Layout Optimization


For any form with a nontrivial number of controls, the layout and corresponding initialization code generated by the Windows Forms Designer into InitializeComponent can become quite involved, and can have a detrimental effect on performance because the form refreshes itself visually as each control is added to its Controls property.

In response to this situation, the Windows Forms Designer employs a special optimization that relies on two methods: SuspendLayout and ResumeLayout. Calls to these methods are placed in InitializeComponent for any form that has at least one control:

// ContainmentForm.cs partial class ContainmentForm {   ...   void InitializeComponent() {     // Hosted control and component instantiation     ...   this.SuspendLayout();     // Hosted control, component, and form initialization     ...     // Controls added to form     ...   this.ResumeLayout(true);     ...   } }


By bracketing several tasksthe child control creation and initialization as well as the addition of the controls to the control collectionin SuspendLayout and ResumeLayout, we prevent the form from trying to draw itself until everything is set up. However, SuspendLayout and ResumeLayout operate only one level deep, so if your form hosts controls within container controls like Panel, you also need to call SuspendLayout and ResumeLayout on the panel. You can do this if you need to make a nontrivial set of changes to the form's properties or controls yourself, a situation you'll encounter when the layout system can't do quite what you need.




Windows Forms 2.0 Programming
Windows Forms 2.0 Programming (Microsoft .NET Development Series)
ISBN: 0321267966
EAN: 2147483647
Year: 2006
Pages: 216

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net