473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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

Nov 16 '05 #1
3 13673
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]
- mv*@spam.guard.caspershouse.com

<jr*******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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

Nov 16 '05 #2
<jr*******@hotmail.com> 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/>
Nov 16 '05 #3
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
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Sagaert Johan | last post by:
I have made a custom control that draws a rectangle when the mouse is down, and does nothing when the mouse is up. I set/reset a flag in MouseDown/Mouse up and use this to do the drawing in the...
2
by: Jaikumar | last post by:
Hi, 1) I have created one windows application, In the main form ( form1) i have added one usercontrol (usercontrol1), In that user control i am drawing one image. 2) In the UserControl1 i am...
10
by: Jeff | last post by:
I'm just starting to work with UserControls and have a question on the behavoir of exceptions within UserControls and VS.Net 2003. I have noticed that when a UserControl throws an exception from...
20
by: BB | last post by:
Hello all, I am trying to override OnPaint in a custom textbox control (so I can drawstring a caption, etc.). In the code below, I get the "painting the form" message as expected, but not the...
2
by: Try Guy | last post by:
I'm not sure if I really understand this whole ownderdraw thing... i want to create my own Windows Forms control (NOT USERCONTROL)...is it ownerdrawing I should start learning or something else?...
3
by: jrhoads23 | last post by:
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...
4
by: grayaii | last post by:
Hi, I have a simple form that handles all its paint functionality like so: this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); And the entry point to this...
14
by: raylopez99 | last post by:
KeyDown won't work KeyPress fails KeyDown not seen inspired by a poster here:http://tinyurl.com/62d97l I found some interesting stuff, which I reproduce below for newbies like me. The main...
6
by: Tom P. | last post by:
I'm trying to make one of our perennial favorites - The Syntax Color Editor. (Mostly as a learning exercise). I'm wondering if there is a way to capture the Paint event of a textbox so I can...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.