Components Notebook
Home

Notebooks
C#
C++
Agile Hacker
Hardware
Photos

Book Reviews

Create A Drawing Canvas With A User Control

Create a C#, Windows Application Project.

Add a User Control to your project. Right click on the Art Project in the Solution Explorer, then select Add then Add User Control.

This screen shot was from Beta 1. When trying to re-do it with Beta 2 the menu closes when pressing Alt Print Screen. The menu selection is the same in Beta 2.

Name your control "Canvas", and override OnPaint.

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();
} 

You need to build your control before adding it to your Form.

Select the Canvas control in the Toolbox and draw it on your application Form.

Set the BackColor property for you canvas.

Run the application to see that it works.

This Canvas control has many uses, for example an x-y graph. The drawing of the axis and plotting of the points can be built into the OnPaint function. A public function can be added to update the data to be plotted.

C# Notebook


wburris at telusplanet dot net