Panel.Enabled property and the propagation to subitems

  • Thread starter Thread starter Alexander Groß
  • Start date Start date
A

Alexander Groß

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


X-Post, F'Up set to microsoft.public.dotnet.framework.windowsforms
_______________________________________

Alexander Groß
Dipl.-Ing. (BA) für Informationstechnik
(e-mail address removed)
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 looked at your sample program, and I was able to fix
the problem by removing the event handler
chkBEnabled_EnabledChange and moving the line
pnlB.Enabled = chkBEnabled.Checked && chkBEnabled.Enabled;
to the end of btnEnableB_Click. It seems to work fine
that way.

As to why it didn't work the way you had it, it looks to
me like it has to do with the order of events. For
example, I put in an event handler for the EnabledChange
event for the B1 label. When I click the A button once
this event is never raised--when I clicked A a second
time it's raised twice. I'm sorry I don't have a clear
explanation right now of exactly why this is happening,
but I know order of events can cause confusion sometimes.

I hope this helps.

Jean

This posting is provided "AS IS" with no warranties, and
confers no rights.
 
Hi Jean,

| I looked at your sample program, and I was able to fix
| the problem by removing the event handler
| chkBEnabled_EnabledChange and moving the line
| pnlB.Enabled = chkBEnabled.Checked && chkBEnabled.Enabled;
| to the end of btnEnableB_Click.

thanks for the suggested solution. It's working fine for me.

Another solution for this is to force repainting panel B's contents by
invalidating them. Using Invalidate() or Refresh() methods seem to have no
difference. This shows that Enabled properties are propagated downwards in a
correct way, but panel B seems to miss repainting itself. If this is by
design or an error in the implementation of panels I can't tell.

Best regards,

Alex

_______________________________________

Alexander Groß
Dipl.-Ing. (BA) für Informationstechnik
(e-mail address removed)
http://www.it99.org/axl
Wired: +49 (0) 3 41 / 47 84 97 70
Cellular: +49 (0) 1 75 / 410 72 68
ICQ# 36765668
_______________________________________
 
Back
Top