Monday, September 27, 2004

Repaint Bug when Parent Panel is Disabled

Alexander Groß wrote some time ago about a bug with a Panel not repainting after its parent Panel's Enabled property is set to false. He even included a nice sample app that demonstrates the problem.

Here's Alexander's message verbatim:

Hi Dejan,

I am not sure if the behaviour I encounter is a bug or if it's by design.
Maybe you can have a look at the demo and tell me if I understood something wrong or the framwork does not work correctly. My post to Microsoft's newsgroup microsoft.public.dotnet.framework.windowsforms follows. It is not answered until today.

Hello everybody,

I encounter problems with the Enabled-property of panels and the propagation to subitems of the container.

Overview:
On a form there is one panel (named "A"), which surrounds all other controls on the form (except two buttons). Inside panel A lies a checkbox. Below this checkbox (and also inside A) panel B is located. The contents of panel B should only be enabled if the checkbox is checked and enabled.

To implement this behaviour I attached an event handler to the CheckedChanged-event of the checkbox. This handler enables panel B if the checkbox is checked. A handler for the EnabledChanged-event sets panel B enabled if the checkbox is checked and also enabled.

Below panel A there are two buttons:
- Button A toggles the Enabled-property of panel A. Panel A also changes the Enabled-property of its contained controls (checkbox and panel B). This propagation should be possible due to the fact that it is documented on MSDN.
- Button B toggles the Enabled-property of the checkbox. The EnabledChanged-event of the checkbox is fired and Panel B's Enabled-property is also toggled.

So far so good. The problem is, that it is not working as expected.
- Initially all controls are enabled and the checkbox is checked.
- If you click button B, the checkbox and panel B are disabled. A repeated click on button B enables them again. Correct.
- If button A is clicked, panel A is disabled. This deactivation is propagated to the checkbox, which is disabled and then fires EnabledChanged.
The handler disables panel B as well. But panel B's contents are not redrawn (i.e. the controls remain visually enabled). If you force to redraw panel B's contents (by dragging the window out of the screen area) the correct state is displayed.

Did I misunderstand the meaning of the Enabled-property or is this behaviour by design?

For illustration purposes I wrote a little demo. Feel free to download the sourcecode at http://it99.dyndns.org/axl/forms.zip

Best regards,
Alex

PS: Your site is great. I enjoyed reading the blog very much :)

Alexander Groß
Dipl.-Ing. (BA) für Informationstechnik
AlexanderGross@gmx.de
http://www.it99.org/axl
Wired: +49 (0) 3 41 / 47 84 97 70
Cellular: +49 (0) 1 75 / 410 72 68
ICQ# 36765668

I've added this as bug #23 in the bug registry.

Thanks Alexander!

   10:55 AM

Content of this site is © Dejan Jelovic. All rights reserved.