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. Check presence of event handler attached to base class event

Check presence of event handler attached to base class event

Scheduled Pinned Locked Moved C#
graphicsquestion
13 Posts 5 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.
  • J Offline
    J Offline
    jjansen
    wrote on last edited by
    #1

    I want to allow a user of my user control to overrule the default painting of my control through a Paint event handler. I was thinking along the lines of... protected override void OnPaint(PaintEventArgs e) { if (this.Paint != null) // won't work; no access base.OnPaint(e); else this.MyDefaultPainting(e); } This doesn't work because the Paint event belongs to the base class and my derived class can't access it (as far as I know). I don't want to create another drawing event next to the Paint event and I don't want a construction that to forces an override of MyDefaultPainting either. More importantly, I don't want my default drawing done in the first Paint event handler in line that may be followed by the user's to prevent redundant painting. So, is there a way to see whether or not an event handler is attached to an event that is defined in a base class?

    M J J A 4 Replies Last reply
    0
    • J jjansen

      I want to allow a user of my user control to overrule the default painting of my control through a Paint event handler. I was thinking along the lines of... protected override void OnPaint(PaintEventArgs e) { if (this.Paint != null) // won't work; no access base.OnPaint(e); else this.MyDefaultPainting(e); } This doesn't work because the Paint event belongs to the base class and my derived class can't access it (as far as I know). I don't want to create another drawing event next to the Paint event and I don't want a construction that to forces an override of MyDefaultPainting either. More importantly, I don't want my default drawing done in the first Paint event handler in line that may be followed by the user's to prevent redundant painting. So, is there a way to see whether or not an event handler is attached to an event that is defined in a base class?

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      if(base.Paint!=null) //should work All the best, Martin

      J 1 Reply Last reply
      0
      • M Martin 0

        if(base.Paint!=null) //should work All the best, Martin

        J Offline
        J Offline
        jjansen
        wrote on last edited by
        #3

        Nope. Same problem as if (this.Paint != null) Thanks for the reply, though.

        M 1 Reply Last reply
        0
        • J jjansen

          I want to allow a user of my user control to overrule the default painting of my control through a Paint event handler. I was thinking along the lines of... protected override void OnPaint(PaintEventArgs e) { if (this.Paint != null) // won't work; no access base.OnPaint(e); else this.MyDefaultPainting(e); } This doesn't work because the Paint event belongs to the base class and my derived class can't access it (as far as I know). I don't want to create another drawing event next to the Paint event and I don't want a construction that to forces an override of MyDefaultPainting either. More importantly, I don't want my default drawing done in the first Paint event handler in line that may be followed by the user's to prevent redundant painting. So, is there a way to see whether or not an event handler is attached to an event that is defined in a base class?

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          Have you tried this.Paint.GetInvokationList() Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

          J 1 Reply Last reply
          0
          • J J4amieC

            Have you tried this.Paint.GetInvokationList() Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

            J Offline
            J Offline
            jjansen
            wrote on last edited by
            #5

            Same problem: not allowed. The event-wrapper prevents me from doing anything but adding or removing an event handler.

            1 Reply Last reply
            0
            • J jjansen

              Nope. Same problem as if (this.Paint != null) Thanks for the reply, though.

              M Offline
              M Offline
              Martin 0
              wrote on last edited by
              #6

              Can you tell me more about your constilation of the UserControl? Does the base UserControl also override the OnPaint? ....

              J 1 Reply Last reply
              0
              • M Martin 0

                Can you tell me more about your constilation of the UserControl? Does the base UserControl also override the OnPaint? ....

                J Offline
                J Offline
                jjansen
                wrote on last edited by
                #7

                No, it doesn't. My control is the first to override this method.

                M 1 Reply Last reply
                0
                • J jjansen

                  No, it doesn't. My control is the first to override this method.

                  M Offline
                  M Offline
                  Martin 0
                  wrote on last edited by
                  #8

                  Just for testing: Have you tried to put the code in your base class?

                  J 1 Reply Last reply
                  0
                  • M Martin 0

                    Just for testing: Have you tried to put the code in your base class?

                    J Offline
                    J Offline
                    jjansen
                    wrote on last edited by
                    #9

                    I can't, it isn't mine. The base class is UserControl.

                    1 Reply Last reply
                    0
                    • J jjansen

                      I want to allow a user of my user control to overrule the default painting of my control through a Paint event handler. I was thinking along the lines of... protected override void OnPaint(PaintEventArgs e) { if (this.Paint != null) // won't work; no access base.OnPaint(e); else this.MyDefaultPainting(e); } This doesn't work because the Paint event belongs to the base class and my derived class can't access it (as far as I know). I don't want to create another drawing event next to the Paint event and I don't want a construction that to forces an override of MyDefaultPainting either. More importantly, I don't want my default drawing done in the first Paint event handler in line that may be followed by the user's to prevent redundant painting. So, is there a way to see whether or not an event handler is attached to an event that is defined in a base class?

                      J Offline
                      J Offline
                      Josh Smith
                      wrote on last edited by
                      #10

                      Use a little reflection to get the field which represents the Paint event, then (from within your control) access the protected Events property. Pass in the event field id and check if it returns null or not.

                      FieldInfo fi = typeof(Control).GetField( "EventPaint", BindingFlags.Static | BindingFlags.NonPublic );
                      object eventPaint = fi.GetValue( this );

                      this.Paint += new PaintEventHandler(Form1_Paint);
                      if( base.Events[eventPaint] == null )
                      {
                      Debug.WriteLine( "No handlers attached to the Paint event." );
                      }
                      else
                      {
                      Debug.WriteLine( "One or more handler is attached to the Paint event." );
                      }

                      :josh: My WPF Blog[^]

                      J 1 Reply Last reply
                      0
                      • J jjansen

                        I want to allow a user of my user control to overrule the default painting of my control through a Paint event handler. I was thinking along the lines of... protected override void OnPaint(PaintEventArgs e) { if (this.Paint != null) // won't work; no access base.OnPaint(e); else this.MyDefaultPainting(e); } This doesn't work because the Paint event belongs to the base class and my derived class can't access it (as far as I know). I don't want to create another drawing event next to the Paint event and I don't want a construction that to forces an override of MyDefaultPainting either. More importantly, I don't want my default drawing done in the first Paint event handler in line that may be followed by the user's to prevent redundant painting. So, is there a way to see whether or not an event handler is attached to an event that is defined in a base class?

                        A Offline
                        A Offline
                        Andrew Lygin
                        wrote on last edited by
                        #11

                        Just define your own Paint event with the "new" keyword and add logic to "add" and "remove" accessors. For example: private PaintEventHandler myPaint; public new event PaintEventHandler Paint { add { base.Paint += value; myPaint += value; } remove { base.Paint -= value; myPaint -= value; } } protected override void OnPaint(PaintEventArgs e) { if (myPaint != null) base.OnPaint(e); else MyDefaultPainting(e); }

                        J 1 Reply Last reply
                        0
                        • J Josh Smith

                          Use a little reflection to get the field which represents the Paint event, then (from within your control) access the protected Events property. Pass in the event field id and check if it returns null or not.

                          FieldInfo fi = typeof(Control).GetField( "EventPaint", BindingFlags.Static | BindingFlags.NonPublic );
                          object eventPaint = fi.GetValue( this );

                          this.Paint += new PaintEventHandler(Form1_Paint);
                          if( base.Events[eventPaint] == null )
                          {
                          Debug.WriteLine( "No handlers attached to the Paint event." );
                          }
                          else
                          {
                          Debug.WriteLine( "One or more handler is attached to the Paint event." );
                          }

                          :josh: My WPF Blog[^]

                          J Offline
                          J Offline
                          jjansen
                          wrote on last edited by
                          #12

                          Josh Smith wrote:

                          Use a little reflection to get the field which represents the Paint event, then (from within your control) access the protected Events property.

                          This is exactly what I need! I knew of the Events property but didn't know where to get the key to retrieve the right event. Thanks, Josh.

                          1 Reply Last reply
                          0
                          • A Andrew Lygin

                            Just define your own Paint event with the "new" keyword and add logic to "add" and "remove" accessors. For example: private PaintEventHandler myPaint; public new event PaintEventHandler Paint { add { base.Paint += value; myPaint += value; } remove { base.Paint -= value; myPaint -= value; } } protected override void OnPaint(PaintEventArgs e) { if (myPaint != null) base.OnPaint(e); else MyDefaultPainting(e); }

                            J Offline
                            J Offline
                            jjansen
                            wrote on last edited by
                            #13

                            Andrew Lygin wrote:

                            Just define your own Paint event with the "new" keyword and add logic to "add" and "remove" accessors.

                            Perhaps a bit overkill if you just want some information about an event, but worth considering if you want to manipulate the event (although I suppose you don't really want to mess too much with a crucial event like Paint). Thanks anyway, Andrew. I'm sure your it'll come in handy someday.

                            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