Click here to Skip to main content
15,938,682 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how can i draw multiple rectangles on a form without loosing the first one. In other words I need to keep each and every rectangle in the memory but how?
Posted
Comments
William Winner 24-May-10 14:52pm    
don't repost your question. If you want to change the wording, then do so in the original post. Reposting it makes people think you haven't gotten any answers yet, when you have.
William Winner 24-May-10 14:58pm    
Reason for my vote of 1
repost
Christian Graus 24-May-10 19:36pm    
Reason for my vote of 1
oP is stupid. Deleted question the answer was for, making the post confusing, is too stupid to post his broken code, etc.

Christian is correct, but perhaps a little blunt!

In order to draw anything persistant, you have to draw it every time the control (and forms are controls, so are panels) needs to be drawn, or thinks it might need to be drawn.

It does this by means of the Paint event. If you handle it, then whatever you draw gets drawn every time it is needed. So if a different app moves over the top of yours, yours gets redrawn when the other app reveals a part of it.
List<Rectangle> listOfRectangles = new List<Rectangle>();
...
private void MyControl_Paint(object sender, PaintEventArgs e)
    {
    Graphics g = e.Graphics;
    foreach (Rectangle r in listOfRectangles)
        {
        g.FillRectangle(brush, r);
        }
    }


To force a redraw, use the Invalidate method - every control has one.

For the moment, ignore the CreateGraphics method - it contains a whole bag full of bags of worms until you understand what you are doing!
 
Share this answer
 
Comments
Christian Graus 24-May-10 14:58pm    
Blunt ? :P in my defense, posting code samples is hard, on the iPad.
TolgaCiftci 24-May-10 15:23pm    
Thank you for the advice before checking in I was doing the exact same thing but it is not letting me do it. I am getting an error saying a local variable named 'rect' cannot be declared in this scope!!!

here is the code:

private void ImageBox_Paint_1(object sender, PaintEventArgs e)
{

RecList.Add(rect);
RecList = RecList.Distinct().ToList();
rect = new Rectangle(buttcorrs[0], buttcorrs[1], buttcorrs[3] - buttcorrs[1], buttcorrs[2] - buttcorrs[0]);

foreach (Rectangle rect in RecList)
{
e.Graphics.FillRectangle(Brushes.Green, rect);
}
It's only because I've seen code like yours, that I know. In future, post code if you want people to fix it,

You are calling creategraphics in your code. This is wrong, unless you WANT to draw a temporary rectangle. To draw permenant ANYTHING, handle the paint event and keep a list of rectangles or whatever you want to draw there. Call invalidate to trigger a paint event.
 
Share this answer
 
v2
Comments
OriginalGriff 24-May-10 15:08pm    
So, are you saying "posting code on the iPad sucks" or "code on the iPad sucks"? :laugh:
Christian Graus 24-May-10 19:55pm    
Having a less than full keyboard does not make for writing code on the fly.
First of all, why would you mark an answer as having solved your issue when you still have questions about it?

Secondly, you're adding a Rectangle before creating it. Your order is all messed up here:

C#
RecList.Add(rect); 
RecList = RecList.Distinct().ToList(); 
rect = new Rectangle(buttcorrs[0], buttcorrs[1], buttcorrs[3] - buttcorrs[1], buttcorrs[2] - buttcorrs[0]); 


If you don't understand the basics (ie. you have to create something before you can add it to something else) then you need to take some steps backwards and start at the beginning.

I do like that you're making sure there aren't any duplicates.

Try switching the last line to before RecList.Add(rect).

Actually, I just saw what the problem was with your comment.

You have a globally declared variable called rect.

Then, you do
C#
foreach (Rectangle rect in...)


you can't declare another variable with the same name as one that's global. try a different name.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900