tags:

views:

636

answers:

2

Hi,

I have a winforms form which is inherited from another form.

e.g.

class StartForm : aSyncDialog

aSyncDialog has an onload event

protected override void OnLoad(EventArgs e)

I have noticed that the load event in StartForm is not firing but the OnLoad one is.

private void StartForm_Load(object sender, EventArgs e)

Any idea why? Is there something I need to put into either the parent or sub class to get it to run?

+3  A: 

Make sure you call base.OnLoad(e) from your override of OnLoad in aSyncDialog

The reason for this is that the OnLoad method in the Form class raises the Load event.

When you override the OnLoad method in aSyncDialog and don't call base.OnLoad, then the event isn't raised, so the subclass of aSyncDialog doesn't have any event to handle.

Patrick McDonald
Actually it's base.OnLoad(e) in C#
Thomas Levesque
Thanks Thomas, can't get VB out of my head
Patrick McDonald
I get the error:"The name 'MyBase' does not exist in the current context"
Ace Grace
Sorry saw the correction after I posted the previous comment.That works!
Ace Grace
+2  A: 

Inside the System.Windows.Form class, the method OnLoad is what actually calls all the event handlers which are hooked up to the Load event. Since you are overriding the implementation of OnLoad, the event handlers are never getting called (StartForm_Load in your case).

To make this work correctly, you need to call Base.OnLoad as Patrick suggests. As a matter of practice, you should always call the base method when overriding, unless you know you specifically don't want the base method to run.

TheSean