PC Review


Reply
Thread Tools Rate Thread

Creating a UserControl... use OnPaint method or capture Paint event?

 
 
jrhoads23@hotmail.com
Guest
Posts: n/a
 
      12th Jan 2005
I have been wondering this for some time. If I create my own class
inherited from an existing class or create a user control, should I be
overriding the OnFontChanged method (or similar) or capturing the
FontChanged event. It seems they both get the same accomplished - so
what is the difference between the two? Which should I be using?
For example, lets say I create a class MyButton which inherits
System.Windows.Forms.Button. Which should I use...

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)

' my code here

MyBase.OnPaint(e)
End Sub

---OR---

Private Sub Me_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
' my code here

End Sub

 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      12th Jan 2005
I would say to override the OnPaint method. It just seems cleaner, when
thinking about other controls subclassing your control, as well as the fact
that you get your work in before any event fires.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have been wondering this for some time. If I create my own class
> inherited from an existing class or create a user control, should I be
> overriding the OnFontChanged method (or similar) or capturing the
> FontChanged event. It seems they both get the same accomplished - so
> what is the difference between the two? Which should I be using?
> For example, lets say I create a class MyButton which inherits
> System.Windows.Forms.Button. Which should I use...
>
> Protected Overrides Sub OnPaint(ByVal e As
> System.Windows.Forms.PaintEventArgs)
>
> ' my code here
>
> MyBase.OnPaint(e)
> End Sub
>
> ---OR---
>
> Private Sub Me_Paint(ByVal sender As Object, ByVal e As
> System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
> ' my code here
>
> End Sub
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      12th Jan 2005
<(E-Mail Removed)> schrieb:
>I have been wondering this for some time. If I create my own class
> inherited from an existing class or create a user control, should I be
> overriding the OnFontChanged method (or similar) or capturing the
> FontChanged event. It seems they both get the same accomplished - so
> what is the difference between the two? Which should I be using?


To Override Or To Handle?
<URL:http://www.bobpowell.net/overrideorhandle.htm>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
 
Reply With Quote
 
Benoit Vreuninckx
Guest
Posts: n/a
 
      12th Jan 2005
Nicholas Paldino [.NET/C# MVP] wrote:
> I would say to override the OnPaint method. It just seems cleaner, when
> thinking about other controls subclassing your control, as well as the fact
> that you get your work in before any event fires.
>
> Hope this helps.
>
>


Hi,

In this example, OnPaint, you should call the base implementation first
as it does the default painting, unless you're going to draw everything
yourself. By doing that, the event handlers will get invoked before
your overriding OnPaint implementation is executed. Calling the base
after you've done your drawing, would probably erase everything (by the
base). In this case the order of execution is important, yet not fully
controllable. Most of the time, the order isn't that important.
To conclude, when subclassing you should override, it's cleaner, it's
more OO (and it's a little faster FWIW). Calling the base before or
after your overriding implementation (or not at all) depends on the
situation. The event exists (only) for external clients, which can't
override the method.

Cheers,
Benoit
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Override OnPaint vs Handle Paint event sanjusunny@gmail.com Microsoft Dot NET Framework Forms 4 6th Oct 2006 01:10 AM
Paint event not firing on usercontrol!!! Peter Row Microsoft C# .NET 2 17th Jul 2006 06:23 PM
Creating a UserControl... use OnPaint method or capture Paint event? jrhoads23@hotmail.com Microsoft VB .NET 3 12th Jan 2005 03:46 PM
Creating a UserControl... use OnPaint method or capture Paint event? jrhoads23@hotmail.com Microsoft Dot NET Framework Forms 3 12th Jan 2005 03:46 PM
How do I capture the paint event on a treeview? Benny Raymond Microsoft C# .NET 1 7th Nov 2004 11:39 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:49 PM.