all groups > dotnet windows forms > july 2005 >
You're in the

dotnet windows forms

group:

How to call base buttons onclick event



How to call base buttons onclick event Adam
7/30/2005 9:28:34 AM
dotnet windows forms: 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");
}
Re: How to call base buttons onclick event Adam
7/30/2005 12:43:59 PM
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.
Re: How to call base buttons onclick event Jon Skeet [C# MVP]
7/30/2005 8:00:31 PM
[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Re: How to call base buttons onclick event Jon Skeet [C# MVP]
7/30/2005 8:53:21 PM
[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Re: How to call base buttons onclick event Jon Skeet [C# MVP]
7/30/2005 9:00:53 PM
[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Re: How to call base buttons onclick event Adam
8/1/2005 8:46:32 PM
I probably misunderstood your first comment. I removed the handler in
the derived class and everything works fine.

Thanks!
Re: How to call base buttons onclick event Adam
8/1/2005 8:48:22 PM
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!
AddThis Social Bookmark Button