I have a base class (itself inheriting from Panel) and an inherited class. The base class has a Click event-handler.
What is the correct way to make a Click event-handler in the inherited class fire before the base’s handler, and only then execute the base’s?
My workaround is of the following form:
class first : Panel
{
public first()
{
Click += first_Click;
}
protected virtual void first_Click(object sender, EventArgs e)
{
doHandler();
}
protected void doHandler()
{
MessageBox.Show("first");
}
}
class derived : first
{
protected override void first_Click(object sender, EventArgs e)
{
MessageBox.Show("derived");
doHandler();
}
}
Is there a straightforward way?
Thanks.
There’s no point in listening for your own events. Override the OnClick() method instead.
Or call base.OnClick() first to alter the order.