tags:

views:

40

answers:

2

I have a Panel which hosts a number of child controls in a grid layout. The child controls each consist of a Panel with a PictureBox and a Label. When one of these child controls is clicked it becomes "selected" (which basically entails changing its background to a different color) and an event is fired. In the handler for this event, an image is displayed in a PictureBox on a separate form.

In code, the background of the child control is definitely changed before firing the event, but for some reason it never updates at runtime until after the image has updated in the other Form. I've tried to Invalidate() and Refresh() the child control before firing the event, without effect.

Why is this happening, and what can I do to set it right?

A: 

If I remember right, the methods associated with the event get executed in the same thread where the event was raised, so the UI thread is blocked with executing the picture change in the seperate form. As a solution, use an additional thread (eg. via ThreadPool) to execute the event. By this, the code will continue and the UI can be redrawn.

Femaref
I simplified for the sake of brevity, but in actuality I am executing the event handler on a separate thread. Ulitimately, the PictureBox update has to happen on the UI thread though, and somehow it's still managing to hang things up.
allonym
I accepted this answer, because it turned out I wasn't *always* handling the event on a separate thread. Once I figured that out, it started behaving as expected.-1 to me for simple boneheadedness :\
allonym
Don't worry, we all have that problem from time to time, it happends.
Femaref
A: 

Please try this: once you have changed the background color do Application.DoEvents(); and after that fire the event for the other form.

m0s
I'll give it a try, thanks!
allonym
If Refresh() didn't work then I don't expect DoEvents() to do any better.
Henk Holterman
See this question: http://stackoverflow.com/questions/952906/how-do-i-call-paint-event/952964#952964
Henk Holterman