How to call base buttons onclick event

A

Adam

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

Jon Skeet [C# MVP]

Adam said:
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?

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.
 
A

Adam

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.
 
J

Jon Skeet [C# MVP]

Adam said:
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).

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.
 
J

Jon Skeet [C# MVP]

Adam said:
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).

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.
 
A

Adam

I probably misunderstood your first comment. I removed the handler in
the derived class and everything works fine.

Thanks!
 
A

Adam

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top