If you followed the code from the link above, right under the rectangle declaration put a new List<T> declaration:
Rectangle rect;
List<Rectangle> theRectangles = new List<Rectangle>();
Now add a mouse up handler
private void Form1_MouseUp(object sender, MouseEventArgs e) {
if (rect != null && theRectangles.Contains(rect) == false) {
theRectangles.Add(rect);
}
}
Then change the Paint event to draw all the rectangles
private void Form1_Paint(object sender, PaintEventArgs e) {
// Replace "Color.Red" with any color and repalce "2" with any size you like.
using (Pen pen = new Pen(Color.Red, 2)) {
foreach (Rectangle r in theRectangles) {
e.Graphics.DrawRectangle(pen, r);
}
}
}
It might generate an exception when it's drawing the rectangles if you are adding one at the same time. If so, you'll either have to lock the collection or iterate through it using a for loop.
thank you it's working.......