Reputation: 16310
I have a form FormA
that inherits from Form
. I override OnLoad
as follows:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//Do FormA stuff here
}
Now I derive a second form FormB
from FormA
so that I have the same form layout. However I want to also override OnLoad
but if I do this:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//Do FormB stuff here
//without doing FormA stuff
}
It would call FormA's OnLoad
which I do not want.
In this scenario, how can I call the Form Onload
event from within FormB
?
Upvotes: 0
Views: 153
Reputation: 5523
You can remove the call to base.OnLoad(e)
. However, I strongly advice against that if there's any sort of processing which manipulates the controls, like databinding them.
Visual inheritance does not find much use, you are better off refactoring common functionality into a user control and add that to both forms instead of inheriting one from the other.
Upvotes: 0
Reputation: 156978
You can't with this code. You can't say base.base.OnLoad
for example.
I guess you want to do something like this.
FormA:
protected sealed override void OnLoad(EventArgs e)
{
base.OnLoad(e);
MyOnLoad(e);
}
protected virtual void MyOnLoad(EventArgs e)
{
// FormA onload
}
FormB:
protected override void MyOnLoad(EventArgs e)
{
// FormB onload
}
Here you force to always call Form.OnLoad
, but you can change the OnLoad
behavior per derived class in MyOnLoad
.
Upvotes: 1