Override Paint Method for a PaintBox

May 3 2007 9:38 AM
Hi,
I am trying to write a program where multiple instances of a class will draw themselves onto a PictureBox.  (If there is a better control to use then that would be useful to know!).  The paint method in the main form looks like :

public virtual void pbWorld_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{...

The override in the class of which multiple instances will occur is :

public override void pbWorld_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// Create a local version of the graphics object for the PictureBox.
Graphics g = e.Graphics;
System.Drawing.Pen pen =
new Pen(System.Drawing.Color.Black, 1);
g.DrawRectangle(pen, posX, posY, 2, 2);
}

What I need the program to do is, everytime a new instance is created, update the main PictureBox with the new object, and then everytime the position of that object changes, update the area around the object (so as not to redraw the whole screen).  I assume that I need to create a specific event for this and link it to the overridden paint method?! 

I hope I have included enough info here - if not, just let me know what else you need to know to help me out on this one!

Many thanks,
Bertie.


Answers (2)