Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / August 2005

Tip: Looking for answers? Try searching our database.

How to call base buttons onclick event

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Adam - 30 Jul 2005 17:28 GMT
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");
}
Jon Skeet [C# MVP] - 30 Jul 2005 20:00 GMT
> 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!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.