I am creating a set of User Controls with a lot of custom painting. Is
there a performance hit in doing the painting by handling the Paint
event versus putting the painting code in an overridden OnPaint method?
I will be creating a a few thousand instances of the custom control.

Re: Override OnPaint vs Handle Paint event by Kevin

Kevin
Tue Sep 19 07:41:38 CDT 2006

Yes, there definitely is. It is the OnPaint method that does the painting.
The Paint event is an event raised by this method. So, you already have
additional indirection going on there. In fact, since the OnPaint method is
protected, it is clearly the intent that the Paint event (and other events)
be used by other classes, rather than the one doing the painting. Events are
notifications, and they involve the creation of delegate instances and the
Message Pump. If you override OnPaint, no delegate instances must be
assigned.

To put it more simply, consider the following:

protected virtual void OnPaint(PaintEventArgs e)
{
// Paint the control
// ...
// Raise the Paint event.
if (Paint != null)
Paint(this, e);
}

If you override the above, you do not need to assign a delegate to handle
the event, and the second line of code illustrated (raising the event, or
executing the delegates assigned to it) is not executed.

--
HTH,

Kevin Spencer
Microsoft MVP
Digital Carpenter

A man, a plan, a canal,
a palindrome that has gone to s**t.

<sanjusunny@gmail.com> wrote in message
news:1158654045.082596.45670@d34g2000cwd.googlegroups.com...
>I am creating a set of User Controls with a lot of custom painting. Is
> there a performance hit in doing the painting by handling the Paint
> event versus putting the painting code in an overridden OnPaint method?
> I will be creating a a few thousand instances of the custom control.
>



Re: Override OnPaint vs Handle Paint event by R3al1ty

R3al1ty
Tue Sep 19 08:43:03 CDT 2006

ah thanks Kevin, I was thinking along those lines.

Kevin Spencer wrote:
> Yes, there definitely is. It is the OnPaint method that does the painting.
> The Paint event is an event raised by this method. So, you already have
> additional indirection going on there. In fact, since the OnPaint method is
> protected, it is clearly the intent that the Paint event (and other events)
> be used by other classes, rather than the one doing the painting. Events are
> notifications, and they involve the creation of delegate instances and the
> Message Pump. If you override OnPaint, no delegate instances must be
> assigned.
>
> To put it more simply, consider the following:
>
> protected virtual void OnPaint(PaintEventArgs e)
> {
> // Paint the control
> // ...
> // Raise the Paint event.
> if (Paint != null)
> Paint(this, e);
> }
>
> If you override the above, you do not need to assign a delegate to handle
> the event, and the second line of code illustrated (raising the event, or
> executing the delegates assigned to it) is not executed.
>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
> Digital Carpenter
>
> A man, a plan, a canal,
> a palindrome that has gone to s**t.
>
> <sanjusunny@gmail.com> wrote in message
> news:1158654045.082596.45670@d34g2000cwd.googlegroups.com...
> >I am creating a set of User Controls with a lot of custom painting. Is
> > there a performance hit in doing the painting by handling the Paint
> > event versus putting the painting code in an overridden OnPaint method?
> > I will be creating a a few thousand instances of the custom control.
> >


Re: Override OnPaint vs Handle Paint event by Stoitcho

Stoitcho
Tue Sep 19 10:10:31 CDT 2006

Hi,

I'd suggest to override the OnPaint method. The event is meant to be used
from outside code that can contribute to the control's own visual. As long
as your control doesn't just contribute, but rather this is its own face it
is naturally the painting to be done in the OnPaint override. Further more
you can decide not to fire the Paint event at all, even though I don't see a
reason why you should do this.

As far as it goes to performance hit, I don't think this should be of any
concerns.


--
HTH
Stoitcho Goutsev (100)

<sanjusunny@gmail.com> wrote in message
news:1158654045.082596.45670@d34g2000cwd.googlegroups.com...
>I am creating a set of User Controls with a lot of custom painting. Is
> there a performance hit in doing the painting by handling the Paint
> event versus putting the painting code in an overridden OnPaint method?
> I will be creating a a few thousand instances of the custom control.
>



Re: Override OnPaint vs Handle Paint event by Patrick

Patrick
Thu Oct 05 19:10:41 CDT 2006

In article <#OIJiY#2GHA.1288@TK2MSFTNGP03.phx.gbl>,
stuart.nathan@homecall.co.uk says...
> I'm not sure there is any difference except to say that it is often
> recommentd to use overrides in a usercontrol

"Avoid subscribing to your own events"
http://weblogs.asp.net/savanness/archive/2003/03/10/3646.aspx

--
Patrick Steele
http://weblogs.asp.net/psteele