How to call the default event handler?
-
Hi, I'm a newbie in the C# world, having arrived from MFC land. I'm learning about C# and .NET by playing around with a Windows Form app. I'm also in the process of porting one of my CP articles to .NET. Anyway, my question: I've added an event handler to a control in the form, and would like it to do nothing in some circumstances -- in other words I want it to just call the parent's event handler. How is that done when it comes to events and delegates? For example, I know that with MFC, if override the IDOK handler (OnOK), I can just call CDialog::OnOK to let it do its default behavior. But events in C#/.NET seem to work differently. I appreciate your help. Regards, Alvaro
When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com
-
Hi, I'm a newbie in the C# world, having arrived from MFC land. I'm learning about C# and .NET by playing around with a Windows Form app. I'm also in the process of porting one of my CP articles to .NET. Anyway, my question: I've added an event handler to a control in the form, and would like it to do nothing in some circumstances -- in other words I want it to just call the parent's event handler. How is that done when it comes to events and delegates? For example, I know that with MFC, if override the IDOK handler (OnOK), I can just call CDialog::OnOK to let it do its default behavior. But events in C#/.NET seem to work differently. I appreciate your help. Regards, Alvaro
When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com
Rememebr that you must override the method that generates the event. You're not in fact overriding the event. For example the OnClick method raises the Click event. The OnClick method is virtual and therefore can be overriden. To call the default you simply would call the base's OnClick method ie base.OnClick(). For example: protected override void OnClick ( System.EventArgs e ) { base.OnClick(e); }
-
Rememebr that you must override the method that generates the event. You're not in fact overriding the event. For example the OnClick method raises the Click event. The OnClick method is virtual and therefore can be overriden. To call the default you simply would call the base's OnClick method ie base.OnClick(). For example: protected override void OnClick ( System.EventArgs e ) { base.OnClick(e); }
Thanks nero. That clears up a lot of things. I wasn't aware of the On_Whatever_ members, but now it makes sense. They get called whenever something happens and their job is to trigger their specific events via their respective delegate members. Cool! So now then, let's suppose I have a form, and I decide to handle the Click event of one of my buttons. In this case, the code inside the OnClick method will execute independently. In other words, my event handler is just a notification that something happened; it doesn't give me any control over whether to still execute the parent's code or not. But what if I did want that type of control? Would I have to derive my own Button class and override the OnClick? Thanks again, Alvaro
When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com
-
Hi, I'm a newbie in the C# world, having arrived from MFC land. I'm learning about C# and .NET by playing around with a Windows Form app. I'm also in the process of porting one of my CP articles to .NET. Anyway, my question: I've added an event handler to a control in the form, and would like it to do nothing in some circumstances -- in other words I want it to just call the parent's event handler. How is that done when it comes to events and delegates? For example, I know that with MFC, if override the IDOK handler (OnOK), I can just call CDialog::OnOK to let it do its default behavior. But events in C#/.NET seem to work differently. I appreciate your help. Regards, Alvaro
When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com
Alvaro Mendez wrote: would like it to do nothing in some circumstances -- in other words I want it to just call the parent's event handler. The pattern Microsoft uses (in all instances I know of) is the following: Class Foo exposes an event named Bar, Foo also has a protected method called OnBar. OnBar does two different things, one is a side-effect of the other. First, in the class that defines the event OnBar can include some processing but to my knowledge they just fire the event, leaving the processing to however that class calls OnBar. The Bar event is fired from within OnBar because events can only be fired while operating within the class that defines it, anywhere else you can only add or remove handlers. The second thing that OnBar does is allow you to handle the Bar event without having to hookup another event handler, reducing a little overhead (though not much). Because OnBar should only fire the event, you can also call OnBar yourself to fire the event outside of normal circumstances. Another thing to note is that events (for the most part) are notifiers that something has happened so you can only respond to that thing happening. As you suggest later, in order to prevent the Click event from firing in certain circumstances you need to go to the source, the OnClick method, and not call
base.OnClick
in those cases. Hope that makes some sense to you, my thoughts have been jumbled a bit lately. James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation -
Thanks nero. That clears up a lot of things. I wasn't aware of the On_Whatever_ members, but now it makes sense. They get called whenever something happens and their job is to trigger their specific events via their respective delegate members. Cool! So now then, let's suppose I have a form, and I decide to handle the Click event of one of my buttons. In this case, the code inside the OnClick method will execute independently. In other words, my event handler is just a notification that something happened; it doesn't give me any control over whether to still execute the parent's code or not. But what if I did want that type of control? Would I have to derive my own Button class and override the OnClick? Thanks again, Alvaro
When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com
It would be a mistake for you not to call the parent's code, just as it is in MFC. Can you imagine if you override the OnPaint event and not call the parent's code. That would be dangerous, because the parent code most often than not is critical for it to function properly. That being said however, if you really don't want to call the parent's code, then yes, you would have to derive your own Button class and override the OnClick. Cheers