Drawing on a Form

The Code

Now that we know there is an OnPaint function, and what the parameters are, add code to override the OnPaint function in your Form class and try it out.

  
    protected override void OnPaint( PaintEventArgs pe )
{
	Graphics g = pe.Graphics;
	Pen myPen = new Pen( Color.Blue, 2 );
	g.DrawLine( myPen, 10, 10, 210, 210 );
	g.DrawLine( myPen, 10, 210, 210, 10 );
	g.DrawEllipse( myPen, 50, 50, 200, 100 );
	myPen.Dispose();
}

  

Here is a screen shot showing the results.

For more information, look in the .NET Framework SDK documentation, in the .NET Framework Reference section.  Look for the Graphics class in the System.Drawing namespace.  Look for the Form class in the System.Windows.Forms namespace.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems.” - Jamie Zawinski