Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding event handler in WinForms

I have a form with a custom control in it. That control has an event handler on the ItemChanged event.

private void ncNomSimple_ItemChanged(object sender, EventArgs e)
{
    some code..
}

I inherit this form, thus I have the control and the event in my new form but I want another event handler in my new form to be called for that event, not the above one. How can I achieve this?

like image 673
Dimitar Tsonev Avatar asked Sep 02 '26 15:09

Dimitar Tsonev


2 Answers

In the base class:

protected virtual void ncNomSimple_ItemChanged(object sender, EventArgs e)
{
    MessageBox.Show("called from Test class");
}

In the derived class:

protected override void ncNomSimple_ItemChanged(object sender, EventArgs e)
{
    MessageBox.Show("called from Test1 class");
}
like image 200
Alex Filipovici Avatar answered Sep 05 '26 06:09

Alex Filipovici


Change the event handler from private to protected virtual and override it in the inherited form.

like image 22
Jamiec Avatar answered Sep 05 '26 06:09

Jamiec