views:

162

answers:

3

There is a timer on the form and in its Tick event there is this line of code:

        this.BackColor = ColorTranslator.FromHtml("#" + ColorCodesBack[_index]);

        CurrentColor = ColorTranslator.FromHtml("#" + ColorCodesFore[_index]);
        SolidBrush sb = new SolidBrush(CurrentColor);

        g.FillEllipse(sb, this.Width/2 -200, this.Height/2 - 200, 200, 200);
        g.DrawImage(b, 150, 150);

The problem is just background changes on every tick and I don't see a Circle on the form.

A: 

You should combine this code with the same tick event that is changing the backgroud. Otherwise you effectively have a race condition as to which "tick" will be fired first. Combining them will solve that problem.

Will Hartung
They're already in same Tick. The first line is changing the background.
Saber
A: 

The suggestion to update the background and the foreground at the same time is solid.

If you cannot control this, perhaps you could add a transparent control to your window on top of the background, and draw onto the transparent control? That way you would always be above in the Z-Order. This would also reduce your need to redraw regularly.

Jason Coyne
Well, I tried to draw the circle on a Panel. But it didn't work.. You've a code sample to draw a circle on a transparent control?
Saber
+2  A: 

What you should do is put your code in the forms Paint event. That will cause your code to redraw ever time the form has to repaint. Like running your mouse over the form or moving the form. Also where are you declaring your graphic object? Because the only way it will be drawn on your form is if you do:

Graphics g = this.CreateGraphics();

If you use the paint event you won't even need a timer object.

norlando02
Thanks, I figured already, and you're right, I should put this code into Paint event.
Saber