views:

735

answers:

2
+2  Q: 

Override OnPaint

If I override OnPaint and draw a square on the control, how do I get that e.graphics.draw... to show up when I'm previewing it in the designer?

+5  A: 

http://msdn.microsoft.com/en-us/magazine/cc164048.aspx

http://msdn.microsoft.com/en-us/magazine/cc164145.aspx

While you could manually register with Control.OnPaint to add your design time UI, you'll find that overriding OnPaintAdornments is a better option because it is only called after the control's design-time/run-time UI is painted, letting you put the icing on the cake (see Figure 20). Simply adding DesignerAttribute to the ClockControl class completes the association: Copy Code

[ Designer(typeof(ClockControlDesigner)) ] class ClockControl : Control { ... }

Jason Coyne
A: 

If you're trying to draw a focus rectangle for you control, and have it be visible when manipulating your control after it has been added to a form, then Gaijin42's approach should work.

If you're trying to see what your custom drawn control looks like...

  • If you've overriden the OnPaint method in the control, you should see the results of that OnPaint when you look at your control after it has been added to a form (or another control).

  • If you're trying to see the results of the Paint when looking at the designer for the control itself, I don't think that is possible (at least it wasn't with VS2005):

    Unfortunately this is not possible. While you're writing the behaviours of the user control the code isn't compiled and the designer actually uses an ordinary user control to host the child controls. The OnPaint override etc will not run.

    To debug the overrides in your control you need to run it in some sort of host. VS2005 provides a new control host for this purpose but I find that even this is not sufficient sometimes.

Daniel LeCheminant