Do not create graphics! (This is a rule of thumb, but valid in 99.90% cases.)
This topic was discussed many times. Always use
Paint
event:
myControl.Paint += delegate(object sender, PaintEventArgs eventArgs) {
using (Pen myPen = new Pen(System.Drawing.Color.Black, 5)) {
Rectangle myRectangle = new Rectangle(20, 200, 40, 80);
eventArgs.Graphics.DrawRectangle(myPen, myRectangle);
DrawRectangleDetail(eventArgs.Graphics, myRectangle);
DrawOnTopOfRectangle(eventArgs.Graphics, myRectangle);
DrawSomethingElse(eventArgs.Graphics);
}
};
Also, where do you call
myPen.Dispose
? In the code above it is shown: through the
using
construct (see comment).
Now, about your methods
Draw1
,
Draw2
, etc. Call them all from the handler show above. Make them accepting a
Graphics
parameter, if you need to re-use already initialized Rectange, add a Rectangle parameter to those which require that. See the calls to
Draw...
in my sample.
—SA