Draw graphics on a form c#


I'm creating a winforms app and need to draw on the for and other controls, but i can't get the graphics to work - nothing displays (except a button i've added). here is my code

public Form1()
      Graphics chartGraphics = CreateGraphics();
      Pen pen = new Pen(Color.Black, 5);
      chartGraphics.DrawEllipse(pen, 50, 50, 50, 50);

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DarrenSenior Software EngineerCommented:
   protected override void OnPaint(PaintEventArgs pe)

        Graphics g = pe.Graphics ;
        Pen pn = new Pen( Color.Blue );
        // Rectangle rect = new Rectangle(50, 50, 200, 100);
        Point pt1 = new Point( 30, 30);
        Point pt2 = new Point( 110, 100);
        g.DrawLine( pn, pt1, pt2 );


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DarrenSenior Software EngineerCommented:
Hi Again,

You cannot call this method in the Load event handler. The drawn content will not be redrawn if the form is resized or obscured by another form. To make your content automatically repaint, you should override the OnPaint method.




Anurag ThakurTechnical ManagerCommented:
have a look at the following links
might be useful

Graphics Programming Using C#

C# / C Sharp » 2D Graphics » Mouse Draw
IndigoTeamAuthor Commented:
thanks a lot
IndigoTeamAuthor Commented:
thanks all
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.