C# Invalidate() only one object

tjmudder

Member
Dec 16, 2006
77
0
0
I'm writing a program for my class that draws a picture. The picture right now is of a house with a some windows and a door and a bird gif the flies by. I want the door to display a random color each time the program is loaded and it worked until I added the bird to fly by. Since the way to get the bird to animate I have to Invalidate() each time, this causes the door to change colors each Invalidate(). So I was wondering if there is a way to separate the the door so that it picks a random color once and keeps displaying that color while the bird flies by.
 

Specialk92282

Member
Nov 15, 2004
143
0
0
Sounds like you have the random color generation in your draw function. Put it in your form load function.
 

tjmudder

Member
Dec 16, 2006
77
0
0
Well I just tried that and I cannot get it to work in my form load function method. Is there anyway you could give me any sample code?

Here is what I'm using:

Random r = new Random();
Color color = Color.FromArgb(r.Next(256), r.Next(256), r.Next(256));
Brush doorColor = new SolidBrush(color);
Graphics g = e.Graphics;
g.FillRectangle(doorColor, 230, 240, 40, 60);

Thanks!

 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
So remove the code that generates the color and put that in your Constructor/Initialize function and reuse the same color everytime you call FillRectangle(...)