1. Home
  2. Computing & Technology
  3. Delphi Programming

Disabling Container Child Controls when the Enabled Property Changes
Fixing Panel's, GroupBox's, "Container's" Enabled property

By , About.com Guide

Every Delphi control exposes the Enabled property. The Enabled property controls whether the control responds to mouse, keyboard, and timer events.

When Enabled is set to False, the control is "not" available to the user - it is visible but will not receive mouse or keyboard input.

Controls such as Label, Edit of Memo will appear dimmed (grayed) when their Enabled property is False.

When an edit box or a label is placed on a panel, if the Enabled property for the panel is set to False, the label and the edit box will *not* appear grayed - as you would expect!

Read on to learn how to "fix" this Delphi container controls behavior.

Container Type Controls

For example, the Panel, GroupBox and the TabPage on the PageControl are so-called container controls.

Container controls are user interface element you can use to group controls together. Controls placed on a container are child controls to their container. The container is also their Parent.

For example, if an application includes a label and an edit box in a group box, the group box is the parent of the label and the edit boc, and the label and the editbox are the child controls of the group box.

Here's a list (partial) of the container type controls you can use in Delphi: TPanel, TGroupBox, TTabControl, TRadioGroup.

As you may notice, to serve as a parent, a control must be an instance of a descendant of TWinControl.
In essence, the TWinControl is the base class for all Delphi control that can accept focus. For example, TEdit is a descendant of the TWinControl, TLabel is not.

All container type controls have the "csAcceptsControls" flag on, in their ControlStyle property.

The ControlStyle property determines various attributes of the control, such as whether the control can receive and respond to mouse clicks.

In general, you will never write to the ControlStyle property of a Delphi control, you should only read it. Control style is set in the constructor of a particular control.

Container Type Controls and the Enabled property

As announced at the beginning of the article, if you place a bunch of controls on a panel or a group box, setting Enabled to false for the panel (or group box) will *not* result in all the child control being grayed out - their Enabled property is not changed.

Note: even though the Enabled property for a child control remains True, if Enabled for a parent is False, you will not be able to set the focus to the child control of a disabled parent.

To actually gray out all the child control of a container control, you need to recursively set the Enabled property to False for every child control.

A custom procedure "EnabledAsParent" does the trick of setting the value of the Enabled property of a child control to match the value of the Enabled property of the parent.

procedure EnabledAsParent(container: TWinControl) ;
var
  index : integer;
  aControl : TControl;
  isContainer : boolean;
begin
  for index := 0 to -1 + container.ControlCount do
  begin
    aControl := container.Controls[index];

    aControl.Enabled := container.Enabled;

    isContainer := (csAcceptsControls in container.Controls[index].ControlStyle) ;

    if (isContainer) AND (aControl is TWinControl) then
    begin
      //recursive for child controls
      EnabledAsParent(TWinControl(container.Controls[index])) ;
    end;
  end;
end;

The Controls property of a TWinControl descendant lists all child controls of a parent control.

The isContainer variable checks for the existance of the csAcceptsControls flag in the ControlStyle property for each child.

Since you can place a panel inside a group box, when disabling the group box you need to make sure all the child control of the panel are also disabled - this is why the EnabledAsParent is called recursivelly, if a child control is a TWinControl descendant.

To actually use the EnabledAsParent procedure, after you set the Enabled property for a container type control, call the EnabledAsParent for that container:

procedure TForm1.CheckBox1Click(Sender: TObject) ;
begin
  GroupBox1.Enabled := CheckBox1.Checked;

  EnabledAsParent(GroupBox1) ;
end;
The Enabled property of the GroupBox1 maps to the value of the Checked property of the check box: CheckBox1.
Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. Disabling Container Child Controls when the Enabled Property Changes (Panel, GroupBox) in Delphi

©2009 About.com, a part of The New York Times Company.

All rights reserved.