... with the help of mouse by clicking on button line should be displayed on panel and if first rectangle should not b disappeared...
Hi,
I don`t understand these words. Can you please elablrate it better?
Thx in advance.
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
And what will be the difference between 1st and 2nd rectangle? Will you create n number of rectanlges (and where to position it/them)?
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
You'll need to save the rectangles in some form of collection and draw them all in the Paint event, otherwise they vanish, as you have seen.
Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
If you followed the code from the link above, right under the rectangle declaration put a new List 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.
Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558