Is there any way to get all the controls on a container control?

Posted by Mason Wheeler on Stack Overflow See other posts from Stack Overflow or by Mason Wheeler
Published on 2009-01-05T23:37:46Z Indexed on 2012/03/25 11:28 UTC
Read the original article Hit count: 215

Filed under:
|
|
|
|

I've got a form with a bunch of controls on it, and I wanted to iterate through all the controls on a certain panel and enable/disable them.

I tried this:

var component: TComponent;
begin
  for component in myPanel do
    (component as TControl).Enabled := Value;
end;

But that did nothing. Turns out all components are in the form's component collection, not their parent object's. So does anyone know if there's any way to get all the controls inside a control? (Besides an ugly workaround like this, which is what I ended up having to do):

var component: TComponent;
begin
  for component in myPanel do
    if (component is TControl) and (TControl(component).parent = myPanel) then
      TControl(component).Enabled := Value;
end;

Someone please tell me there's a better way...

© Stack Overflow or respective owner

Related posts about delphi

Related posts about forms