faqts : Computers : Programming : Languages : C# : Controls

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 4 people (75%) answered Yes
Recently 3 of 4 people (75%) answered Yes

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.