i want to draw a rectangle in windows form in c# with the help of mouse by clicking on button line should be displayed on panel and if first rectangle should not b disappeared...

plzzzzzzzzz rply soon........

... 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.

Hi,
I don`t understand these words. Can you please elablrate it better?
Thx in advance.

i want to draw a rectangle with the help of mouse in windows form c#.when i click on button then it should allow me to draw a rectangle on panel or a form and when i draw the 2nd rectangle the 1st one was not disappear.....

And what will be the difference between 1st and 2nd rectangle? Will you create n number of rectanlges (and where to position it/them)?

And what will be the difference between 1st and 2nd rectangle? Will you create n number of rectanlges (and where to position it/them)?

there is no difference between 1st and 2nd rectangle.the 2nd rectangle should not overwrite the 1st rectangle.i meant as in paint when we select rectangle we can draw multiple rectangles one by one same as i want to draw rectangles in c#windows form.

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.

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.

can you please suggest how to do it????

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.

Edited 5 Years Ago by Momerath: n/a

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.......

This question has already been answered. Start a new discussion instead.