Entry
How do I get the Graphics object in an OnPaint() event?
I need to define how to draw my control, where do I do that?
Why does my control only paint properly sometimes?
Apr 1st, 2002 09:33
Nathan Wallace,
To draw your control you need to override the OnPaint method:
protected override void OnPaint (PaintEventArgs pe) {
base.OnPaint(pe);
Graphics g = pe.Graphics;
// your drawing code here
}
Make sure to get the graphics object from the passed in PaintEventArgs
rather than by creating it from the control class. I assume this is
because this.CreateGraphics() creates a new graphics object all the
time rather than redrawing on the existing object that is available as
pe.Graphics.