Inhetited form event order

S

Sergey Atun

Hi,
I inherited form and I writed it's load event:

public class frmBase: System.Windows.Forms.Form
{
....
private void frmBase_Load(object sender, System.EventArgs e)
{
MessageBox.Show("this event must run after derived class' (frmInherited)
load event");
}
....
}

When I created new form by inherited this, I writed it's load event too.

public class frmInherited : frmBase
{
{
....
private void frmInherited_Load(object sender, System.EventArgs e)
{
MessageBox.Show("this event must run before parent class' (frmBase) load
event");
}
....
}

How can I run firstly frmInherited_Load event and after that run
frmBase_Load event.
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi Sergey,

I recommend you to override "OnLoad(...)" methods.
You'll get access to order of "OnLoad(...)" call.

e.g.

// in frmBase
protected override void OnLoad(EventArgs e) {
// Load event first
base.OnLoad(e);

// your handler's method next
frmBase_Load(this, e);
}

// in frmInherited
protected override void OnLoad(EventArgs e) {
// OnLoad(...) in frmBase first
base.OnLoad(e);

// your handler's method next
frmInherited_Load(this, e);
}

Regards

Marcin
 
G

Guest

ups!

// in frmInherited
protected override void OnLoad(EventArgs e) {
// your handler's method next
frmInherited_Load(this, e);

// OnLoad(...) in frmBase first
base.OnLoad(e);
}

;-)

Marcin
 
R

Robert Jordan

Hi Sergey,
Hi,
I inherited form and I writed it's load event:

public class frmBase: System.Windows.Forms.Form
{
...
private void frmBase_Load(object sender, System.EventArgs e)
{
MessageBox.Show("this event must run after derived class' (frmInherited)
load event");
}
...
}

When I created new form by inherited this, I writed it's load event too.

public class frmInherited : frmBase
{
{
...
private void frmInherited_Load(object sender, System.EventArgs e)
{
MessageBox.Show("this event must run before parent class' (frmBase) load
event");
}
...
}

How can I run firstly frmInherited_Load event and after that run
frmBase_Load event.

You cannot. The calling order of event handlers is undefined.

However, you're probably looking for that:

public class frmBase: System.Windows.Forms.Form
{
protected override OnLoad(EventArgs e) {
base.OnLoad(e);
// your code
}
}

public class frmInherited : frmBase
{
protected override OnLoad(EventArgs e) {
base.OnLoad(e);
// your code
}
}

the "your code" blocks are executed in the expected order.

bye
Rob
 
R

Robert Jordan

Hi Marcin,
Hi Sergey,

I recommend you to override "OnLoad(...)" methods.
You'll get access to order of "OnLoad(...)" call.

e.g.

// in frmBase
protected override void OnLoad(EventArgs e) {
// Load event first
base.OnLoad(e);

// your handler's method next
frmBase_Load(this, e);

this is wrong. the event handler was already called by base.OnLoad()!

bye
Rob
 
G

Guest

Hi Robert,
Hi Marcin,



this is wrong. the event handler was already called by base.OnLoad()!

Ok, my fault!
Only if you add this method as an "EventHandler" of "Load" event.

I forgot to tell that "frmBase_Load" (and "frmInherited_Load") should
be removed from event handling... and put into "OnLoad(...)" body
(with unchanged declaration) as a methods.

So, they can stay as a common methods, not as an event hadlers.

Marcin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top