tags:

views:

344

answers:

4

EDIT -- simplified version to help focus


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()
    {
        try
        {
            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();

        }
        catch (Exception ex)
        {

            throw;
        }

    }

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?

Thanks!

+2  A: 

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

sfossen
Can you elaborate as it relates to my question? I am not sure what you are advising me to do as far as placing this in the Constructor or FormLoad()?
Refracted Paladin
In the constructor, since they are part of the form. Setting variable options in Form load.
sfossen
so cerate the forms in the constructor but do anything else with them in the formLoad(). Thanks
Refracted Paladin
Yes, since they are an integral part of the form, if they were optional I would put them in formLoad()
sfossen
+2  A: 

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

Thank you! I should just leave it as throw then?
Refracted Paladin
You really should never catch (Exception) and re-throw, ever, either; it's pointless.
Robert C. Barth
So I should not even use a try,catch here?
Refracted Paladin
No. Only catch the Exceptions you are going to handle.
Lars Mæhlum
when doing it like above VS tells me "The variable 'ex' is declared but never used..." Is that normal or a new SO thread?
Refracted Paladin
use try { } catch { } or try { } catch(Exception) { /* without variable, only a type */ }
abatishchev
Thanks, I obviously have a ways to go on Exception Handling!
Refracted Paladin
in FormPaint() you shouldn't use try/catch at all
abatishchev
No? I thought I should use it whenever I try to "load" something in case that load fails...incorrect?
Refracted Paladin
+2  A: 

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.

Scott
+1  A: 

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

abatishchev