Form Lifetime

Although the user can't see a form until either Show or ShowDialog is called, a form exists as soon as the object is created. A new form object wakes up in the object's constructor, which the runtime calls when an object is first created. It's during the constructor that InitializeComponent is called and therefore when all the child controls are created and initialized.

It's a bad idea to put custom code into the InitializeComponent function because the Designer is likely to throw it away. However, if you'd like to add other controls or change anything set by the InitializeComponent method, you can do that in the constructor. If the initial form implementation was generated by one of the VS.NET wizards, you'll even have a helpful ...

Get Windows Forms Programming in C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.