Check presence of event handler attached to base class event
-
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? -
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? -
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?Have you tried
this.Paint.GetInvokationList()
Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour -
Have you tried
this.Paint.GetInvokationList()
Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour -
Can you tell me more about your constilation of the UserControl? Does the base UserControl also override the OnPaint? ....
-
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?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[^]
-
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?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); }
-
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[^]
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.
-
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); }
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.