delphi sql server asp java coldfusion web developer training course class uk floridadelphi asp java coldfusion sql server web developer training course class uk floridadelphi sql server asp java coldfusion web developer training course class uk floridadelphi sql server asp java coldfusion web developer training course class uk floridadelphi sql server asp java coldfusion web developer training course class uk floridadelphi sql server asp java coldfusion web developer training course class uk florida
delphi sql server asp java coldfusion web developer training course class uk floridadelphi sql server asp java coldfusion web developer training course class uk floridadelphi sql server asp java coldfusion web developer training course class uk floridadelphi sql server asp java coldfusion web developer training course class uk floridadelphi sql server asp java coldfusion web developer training course class uk florida

 

Enabling & Disabling Child Controls
written by Rick Spence

To enable and disable visual components you just set their enabled property, right? Well yes and no. This works fine for most types of controls, but when working with containers, such as group boxes, panels, tabSheets and the like, the visual effect is probably not what you want.

Try the following. Create a form with a group box and place some controls inside the group box. Drop a push button onto the form, and code its onClick event to set the group box's enabled property false. While the controls inside the group are indeed disabled, in that you cannot switch focus to them or change their contents, they are not grayed out. Having the control grayed is an indication to the user that the control is not available at the moment, so this is unfortunate.

I disable edit controls all the time in my data entry screens. I set the datasource's autoEdit property false so that users cannot start editing data simply by clicking in the controls; they must explicitly request to edit the data. When they cannot edit the data I like to disable the controls, re-enabling them when in edit mode. I could write lots of statements to explicitly enable and disable individual controls, but being the lazy programmer I am, that seems like too much effort. I want to be able to disable or enable a whole set of controls with one piece of code.

They key of course is to place the controls in some sort of container - I usually use tab sheets, but as you just saw, enabling and disabling a container does not gray out the container's child controls. The solution is to loop through all the child controls of a container, disabling each child control explicitly - easy once you know the properties to use.

The abstract (abstract in that you do not use it directly) TWinControl class, from which all containers inherit, defines two properties which allow you to access its children:

ControlCount - the number of child controls
Controls - an array which gives access to these child controls

So, to explicitly disable the children of a panel called panel1, you would write:

For i := 0 To Panel1.ControlCount - 1 Do
Panel1.Controls[i].Enabled := False;

To enable or disable the children of any sub class of TWinControl , use the following method (note this must be a method of the form, not a stand alone sub routine - the code needs to access the properties of the form)

procedure TForm1.EnableDisableChildren(ParentControl: TWinControl;
State: Boolean);
Var
i : Integer;
begin
For i := 0 To ParentControl.ControlCount - 1 Do
Begin
ParentControl.Controls[i].Enabled := State;
End;
end;

and call it like this:

EnableDisableChildren(Panel1, False);

The second parameter, of course, indicates whether to enable or disable the control.

Now, this works for simple cases, but if you have a container, which has another container as one of its children, such as a TabSheet which contains a groupBox, the code just shown will disable the groupBox, but does not loop through the groupBox's children so those controls are not grayed out. The solution is for the routine to check whether the control being disabled has any children, and to explicitly disable those as well. Since we do not know how deep this nesting will be, we must write the routine recursively, as shown below.

procedure TForm1.EnableDisableChildren(ParentControl: TWinControl;
State: Boolean);
Var
i : Integer;
thisControl : TControl;
begin
For i := 0 To ParentControl.ControlCount - 1 Do
Begin
thisControl := ParentControl.Controls[i];
thisControl.Enabled := State;
If (thisControl IS TWinControl) and
(TWinControl(thisControl).ControlCount > 0) Then
EnableDisableChildren( TWinControl(thisControl), State);
End;
end;

Too lazy to type this in? Download the routine and a sample test project from www.DelphiZine.com.

Until next month!