Export (0) Print
Expand All
27 out of 66 rated this helpful - Rate this topic

Code: Drawing a Line on a Form (Visual C#)

Visual Studio .NET 2003

This example demonstrates how to draw a line on a form.

Example

System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.DrawLine(myPen, 0, 0, 200, 200);
myPen.Dispose();
formGraphics.Dispose();

Compiling the Code

This example requires:

  • A Windows Application project.

The code needs to be within the scope of the form class. The instance of the form is represented by this.

Robust Programming

You should always call Dispose on any objects that consume system resources, such as Pen and Graphics objects.

See Also

Graphics Programming Example Topics | Drawing Lines and Shapes with GDI+ | GDI+ Graphics | Graphics.DrawLine Method

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.