Dimitar Tsonev
Dimitar Tsonev

Reputation: 3882

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?

Upvotes: 3

Views: 9530

Answers (3)

Zheng Qiang
Zheng Qiang

Reputation: 178

in the base class, write it as (public/protected)virtual, then you can write the override in the child class, but i do not think base class should be protected. correct me if i wrong

Upvotes: 0

Alex Filipovici
Alex Filipovici

Reputation: 32561

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");
}

Upvotes: 6

Jamiec
Jamiec

Reputation: 136104

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

Upvotes: 3

Related Questions