Search code examples
c#winformsevent-handlingtextchangedvalidating-event

Same event handler for two events which requires different signature


I have a method that I want to invoke both on the TextChanged and Validating events. The problem is that the e parameter of TextChanged is of type EventArgs while the e parameter of Validating is of type CancelEventArgs.

I could obviously do something like this:

void TextBox_TextChanged(object sender, EventArgs e) => Method();
void TextBox_Validating(object sender, CancelEventArgs e) => Method();

but I wonder if there is an option to make both events to have the same event handler.


Solution

  • Since EventArgs is base for CancelEventArgs, you can use (object sender, EventArgs e) for both events. On properties window you may not see proper event name because you use base class so you can write name there manually