Event handler is just the method. It becomes a handler when you add it to the invocation list of some event instance.
Therefore, the concept of "overriding" is not different from overriding of any other method. The method should be
virtual and the derived class need to have sufficient access to it; most typically,
protected
access modifier is used. Only then a method can be overridden.
In your code sample, I cannot see the keywords "
virtual
" or "
abstract
", therefore, there is nothing to override. To change it, you need to make the method virtual, only then you can override it.
If by "overriding" you actually meant something else, please clarify; probably I'll help you to use proper term for what you need, but in this case you have to explain your ultimate goals.
[EDIT]
For example,
abstract class MyBaseForm : Form {
internal MyBaseForm() {
Button.Click =+ ButtonClickHandler;
}
Button Button = new Button;
protected abstract ButtonClickHandler(object sender, EventArgs eventArgs);
}
class MyForm : MyBaseForm {
protected override ButtonClickHandler(object sender, EventArgs eventArgs) {
MessageBox("Do something");
}
}
Note that doing the same with some control class and adding an event handler to the same event makes no practical sense, because, once you override, you can always use alternative handling, not adding an event handler, but overriding already existing virtual method, such as
OnClick
,
OnPaint
, and so on.
—SA