Question

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

I have some code that is calling a custom class that embeds a form in a control. I had originally been declaring my child forms outside the Constructor and then calling a FormPaint() routine in the FormLoad() to then load the forms like so:

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's form FormPaint()
    frmWWCMCPHost.FormPaint();
}

Now I have been shown a much better way of embedding forms in container controls, as it relates to my custom class, here.

My question is where should I be loading these as the example has them being loaded in the Constructor declaring them at the same time, like so:

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

Which is, obviously, much less code. By loading in the constructor will I be using far more unnecessary resources? Will I be gaining anything? How do I decide?

Was it helpful?

Solution

Interesting question Mr_Mom. My recommendation would be to use your constructors to do only the setup needed for the sub forms and put off loading the sub forms until the parent formLoad().

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

OTHER TIPS

I prefer to use form's constructor. I mean setup everything before a form would be shown, not after.

Reducing the outward complexity will increase readability and should reduce possible errors.

On a tangent, never ever rethrow an exception using throw ex; It'll reset the call stack. Just use throw;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top