Draw graphics on a form c#

Hi,

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()
{
      InitializeComponent();
      Graphics chartGraphics = CreateGraphics();
      Pen pen = new Pen(Color.Black, 5);
      chartGraphics.DrawEllipse(pen, 50, 50, 50, 50);
      
      pen.Dispose();
      chartGraphics.Dispose();
}

thanks
IndigoTeamAsked:
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 );

    }
0

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.

See:

http://msdn.microsoft.com/en-us/library/cyh3c8h8.aspx

Cheers,

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

Graphics Programming Using C#
http://www.developer.com/net/csharp/article.php/1435391

C# / C Sharp » 2D Graphics » Mouse Draw
http://www.java2s.com/Code/CSharp/2D-Graphics/DrawingShapes.htm
0
IndigoTeamAuthor Commented:
thanks a lot
0
IndigoTeamAuthor Commented:
thanks all
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.