Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to call the default event handler?

How to call the default event handler?

Scheduled Pinned Locked Moved C#
csharptutorialquestionc++html
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alvaro Mendez
    wrote on last edited by
    #1

    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

    N J 2 Replies Last reply
    0
    • A Alvaro Mendez

      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

      N Offline
      N Offline
      neroknights
      wrote on last edited by
      #2

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

      A 1 Reply Last reply
      0
      • N neroknights

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

        A Offline
        A Offline
        Alvaro Mendez
        wrote on last edited by
        #3

        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

        N 1 Reply Last reply
        0
        • A Alvaro Mendez

          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

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • A Alvaro Mendez

            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

            N Offline
            N Offline
            neroknights
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups