Form Load () or Not to Form Load ()

Should I load child forms in the constructor or FormLoad ()?

I have code that calls a custom class that inserts a form into a control. First, I declared my child forms outside the constructor, and then I called the FormPaint () procedure in FormLoad (), and then I loaded the forms as follows:

internal frmWWCMCPHost frmWWCMCPHost = new frmWWCMCPHost(); internal frmWWCEnrollmentHost frmWWCEnrollmentHost = new frmWWCEnrollmentHost(); internal frmWWCMemberHost frmWWCMemberHost = new frmWWCMemberHost(); public frmWWCModuleHost() { InitializeComponent(); } private void frmWWCModuleHost_Load(object sender, EventArgs e) { FormPaint(); } public void FormPaint() { WinFormCustomHandling.ShowFormInControl(frmWWCMCPHost, ref tpgMCP, FormBorderStyle.FixedToolWindow,-4,-2); WinFormCustomHandling.ShowFormInControl(frmWWCMemberHost, ref tpgMember, FormBorderStyle.FixedToolWindow, -4, -2); WinFormCustomHandling.ShowFormInControl(frmWWCEnrollmentHost, ref tpgEnrollment, FormBorderStyle.FixedToolWindow, -4, -2); // Call each top-Level (visible) tabpage form FormPaint() frmWWCMCPHost.FormPaint(); } 

Now they showed me a much better way to embed forms in container controls, since it belongs to my custom class here .

My question is where should I load them, since in the example they are loaded into the constructor, declaring them at the same time, for example:

 public frmWWCModuleHost() { InitializeComponent(); WinFormCustomHandling.ShowFormInContainerControl(tpgCaseNotes, new XfrmTest()); } 

This is obviously much less code. When loading in the constructor, will I use much more unnecessary resources? Will I get something? How can i decide?

+4
source share
4 answers

Interesting question Mr_Mom. My recommendation would be to use your constructors only for the customization needed for helper forms, and defer loading the subforms to the parent form Load ().

As for resources, gains and losses ... I do not know.

+2
source

I prefer to use the form constructor. I mean setting up everything before , the form will be shown, not after.

+4
source

Reducing external complexity will increase readability and reduce possible errors.

+2
source

On a tangent, never throw an exception with throw ex; Will reset the call stack. Just use throw;

+2
source

All Articles