Winforms Form Constructor vs Load event

When the form loads, the code should do things like installation datagrids, comboboxes, set the header, etc. I usually used the load event, not the new one (constructor). Are there any guidelines for which activities are best suited?

+9
constructor initialization winforms onload-event
Nov 05 '08 at 5:25
source share
3 answers

The InitializeComponent call is automatically inserted into the constructor of your form / page. InitializeComponent is an automatically generated method that

  • creates various user interface elements on winform / XAML page
  • initializes its properties with the values ​​stored in the resource file

So, everything related to customizing / modifying the user interface should go after this call. When you do this in an override of Form.OnLoad, you are sure that the user interface is ready to go (InitializeComponent has been called) ... so I voted to use OnLoad for the user interface.
Creating elements that are not related to the user interface, the constructor will be the place that I first examined.

+2
Nov 05 '08 at 6:38
source share

Keep in mind that anything in the form constructor will be created / executed when these forms are created. those. at:

Form frm = new Form ();

While something in the Load event will happen only when the form is shown , i.e. frm.Show ();

+1
Nov 05 '08 at 9:46
source share

Basically, you want your constructor to be as light as possible. I am trying to put most of the things in a load event handler, as the user interface elements were created and can be used at this time. However, I usually create class objects, etc. In the constructor, since it is actually part of the construction of the object. Sometimes you can’t put things in one place or another, but at the time you can, you just have to put them where it seems most suitable.

0
Nov 05 '08 at 5:43
source share



All Articles