Form not painted. Or is it?

N

Nayan

I have been working on a card game (called Uno) for past few days.
Language used : C# 2.0

When I start a new game, a dialog window pops up asking the name of
the player. I enter the name, and then cards are distributed to the
computer and player. Then I set a flag true, to signal that the client
area is dirty - please paint it. There is a timer which check for this
flag and if it finds it true, it invalidates the form.

Concept :-

.... timer_tick handler code...
{
if (true == paintFlag)
{
...form.Invalidate() ;
paintFlag = false;
}
}

This should invoke the painting handler of my form.

Problem:
I see only the portion of the form painted where the first pop up
window was located (usually in the middle of the window), after the
pop up window is gone.

Why isn't the whole form painted? Or does it? If yes, why are the
cards not visible?

If you want, have a look at the code here -
http://cnayan.googlepages.com/2007-03-17_snapshot-Uno.zip
I know the code in unoptimized right now. :)

Thanks in advance! :)
Nayan
 
M

Michael C

Nayan said:
I have been working on a card game (called Uno) for past few days.
Language used : C# 2.0

When I start a new game, a dialog window pops up asking the name of
the player. I enter the name, and then cards are distributed to the
computer and player. Then I set a flag true, to signal that the client
area is dirty - please paint it. There is a timer which check for this
flag and if it finds it true, it invalidates the form.

Your whole approach is wrong here. Just paint everything in OnPaint and call
form.invalidate when things change. Get rid of the timer.

Michael
 
N

Nayan

Thanks Michael for the suggestion.

I know this is the usual approach. But instead of duplicating code
(form invalidation call) everywhere, I just set a flag which is easier
to do. Timer is ticking without any harm to the code. When it sees the
flag, it just invokes painting, apart from any other task to do.

I can change the code, but in my opinion, it won't solve the problem.
If the code is wrong please help me there. Later on, concept can be
revised/optimized.
First thing first, the program should work 100%.

Thanks a lot!
Nayan
 
C

Christof Nordiek

Hi Nayan,

did you check, if the form.Invalidate-line is really reached?
did you mark the paintFlag variable as volatile?
maybe you have some problem with the memory-model.

Christof
 
N

Nayan

Hi Christof,

I got the problem's solution :)

Actually everything is fine.. except the logic - I was calling
Invalidate() and then was setting the flag to false *somewhere* else
(in Paint method).

I corrected it and have continued the development. Thanks for
trying :)

Nayan
 
M

Michael C

Nayan said:
Thanks Michael for the suggestion.

I know this is the usual approach. But instead of duplicating code
(form invalidation call) everywhere, I just set a flag which is easier
to do.

A tiny bit easier, maybe.
Timer is ticking without any harm to the code. When it sees the
flag, it just invokes painting, apart from any other task to do.

The harm is that is can produce race conditions and potentially difficult to
find bugs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top