Title: Disable all controls in a WinControl
Question: Do you really disable all Controls when disable their parent?
Answer:
To disable all controls in a WinControl is easy using Delphi. Just set WinControl's Enabled property to false, then all controls which on it will be disabled. For example, if you place a button in a panel, then set the panel's Enabled property to false, then the button is disabled. A problem of this is that the button's Enabled property is true, so it looks like enabled, then the user of your program will try to click the button and find no response.
It's a good idea that set these all controls' Enabled property to false, then they will give the user a good look. To do so a way is list all controls, then set their Enabled property to false, such as:
Button1.Enabled := False;
Button2.Enabled := False;
Edit1.Enabled := False;
...
This way is simple but not very well. Because a WinControl has ControlCount and Controls property so use these will be good.
Note since a Control which on a WinControl may be a TWinControl, it's possible that the Control will contain another controls. For example, A tabsheet will contain a panel, and the panel will contain a button. The button is in the panel's Controls propery, not tabsheet's Controls property. To resolve this, I use recursive function.
Here's my function:
/////////////////////////////////////////////////////////////////
// MethodName: EnableControls
// Function : Set all Controls' , which is Child of :AParent, Enabled property
// to :AEnabled
// Author : Chen Jiangyong
// Date : 1999/02/14
/////////////////////////////////////////////////////////////////
procedure EnableControls(AParent: TWinControl; const AEnabled: Boolean);
var
i: Integer;
AWinControl: TWinControl;
begin
with AParent do
for i := 0 to ControlCount - 1 do begin
// Set enabled property
Controls[i].Enabled := AEnabled;
// Set all his children's property of enable
if Controls[i] is TWinControl then begin
AWinControl := TWinControl(Controls[i]);
if AWinControl.ControlCount 0 then
EnableControls(AWinControl, AEnabled);
end
end
end;
/////////////////////////////////////////////////////////////////
// MethodName: EnableAllControls
// Function : Set all Controls' , which is Child of :AParent, Enabled property
// and :Parent itself's Enabled property to :AEnabled
// Author : Chen Jiangyong
// Date : 1999/02/14
/////////////////////////////////////////////////////////////////
procedure EnableAllControls(AParent: TWinControl; const AEnabled: Boolean);
begin
// Set enabled property of all its children
EnableControls(AParent, AEnabled);
// Set enabled property of itself
AParent.Enabled := AEnabled;
end;