Thursday, 28 February 2013

Iterate through controls on a form

Here is how to iterate through all the controls on a form, this is very useful for setting a property value of more than one control, for example the enabled property.

procedure IterateControls(AControl: TControl);
    var
        iControl: Integer;
    begin
        if AControl = nil then
            Exit;
        if AControl is TWinControl then
        begin
            for iControl := 0 to TWinControl(AControl).ControlCount - 1 do
                IterateControls(TWinControl(AControl).Controls[iControl]);
        end;

        // Do Control stuff here
        AControl.Enabled := false;
    end;

Where you want to set the control property value it is possible to check for the class something like this.

if (AControl is TButton) then
   (AControl as TButton).Enabled := false;

And, of course the boolean value can be passed as a parameter to the procedure.

No comments:

Post a Comment