Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw line in c#

I new to c# and I am struggling to draw a line in a form. Here is the code I have so far.

Graphics g;

g = this.CreateGraphics();

Pen myPen = new Pen(Color.Red);
myPen.Width = 30;
g.DrawLine(myPen, 30, 30, 45, 65);

g.DrawLine(myPen, 1, 1, 45, 65);
like image 403
JohnB Avatar asked Mar 11 '11 20:03

JohnB


1 Answers

Try it in OnPaint

protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g;

            g = e.Graphics;

            Pen myPen = new Pen(Color.Red);
            myPen.Width = 30;
            g.DrawLine(myPen, 30, 30, 45, 65);

            g.DrawLine(myPen, 1, 1, 45, 65);
        }
like image 143
Bala R Avatar answered Sep 23 '22 16:09

Bala R