Hello. I have a form which derives from a System.Windows.Forms.Form.
On the derived form, I would like to override a button's onclick event,
and perform some validation on the derived form, then finally call the
base class' onclick event.
I have tried the following with no success. The derived class' event is
being called twice. I know this is because both buttons are wired to
call the onclick event. However, I would like a solution where I don't
have to change the code in the base class.
Should I just remove the base handler from the derived? I could do
this, but then what would be the syntax for calling the base class'
onclick?
Thanks in advance.
base class:
protected virtual void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("in 1");
}
derived class:
protected override void button1_Click(object sender, System.EventArgs
e)
{
MessageBox.Show("in 2");
}
> Hello. I have a form which derives from a System.Windows.Forms.Form.
> On the derived form, I would like to override a button's onclick event,
[quoted text clipped - 9 lines]
> this, but then what would be the syntax for calling the base class'
> onclick?
If you're overriding the method in the derived class, you don't need to
add the handler twice - just add it once, in either of the classes.
Then in the overriding method, call
base.button1_Click();
after you've done whatever validation you need.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Adam - 30 Jul 2005 20:43 GMT
This is fine, but it still causes my events to be called twice. In the
derived form's constructor I tried adding
base.button1.Click -= new System.EventHandler(base.button1_Click);
However, the click event in my derived form still seems to be called
twice (the message box shows twice).
Thanks.
Jon Skeet [C# MVP] - 30 Jul 2005 20:53 GMT
> This is fine, but it still causes my events to be called twice. In the
> derived form's constructor I tried adding
[quoted text clipped - 3 lines]
> However, the click event in my derived form still seems to be called
> twice (the message box shows twice).
Could you post a short but complete program which demonstrates the
problem?
See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP] - 30 Jul 2005 21:00 GMT
> This is fine, but it still causes my events to be called twice. In the
> derived form's constructor I tried adding
[quoted text clipped - 3 lines]
> However, the click event in my derived form still seems to be called
> twice (the message box shows twice).
Thinking about it - try changing the above to:
button1.Click -= new System.EventHandler(button1_Click);
When the event handler is first added, it'll be added with a
polymorphic reference to the derived handler. That's the reference you
need to remove, I suspect!
It would be easier just not to add the second handler though - *just*
override the first one.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Adam - 02 Aug 2005 04:46 GMT
I probably misunderstood your first comment. I removed the handler in
the derived class and everything works fine.
Thanks!
Adam - 02 Aug 2005 04:48 GMT
I probably misunderstood your first comment. I removed the handler in
the derived class, took out the -= code line on the click event and
everything worked fine.
Thanks!