It is quite simple to enumerate Form components
for (int i=0;i<ComponentCount;i++)
{
ShowMessage(Components[i]->Name);
//.....
}
but the same thing does not work if I want to enumerate only the components which are located on Panel.
for (int i=0;i<Panel1->ComponentCount;i++)
{
ShowMessage(Panel1->Components[i]->Name);
//.....
}
because
Panel1->ComponentCount;
is just zero while having several components on Panel. So, how can I enumerate the child components of Panel?
The
ComponentCountandComponents[]properties access a component’s list of owned components – components that have the component set as theirOwnerby having that component passed to their constructor. All components created at design-time have the parentTForm(orTFrameorTDataModule) set as theirOwner. Your first loop is iterating through the TForm’s owned components, that is why it works. Your TPanel does not own any components, that is why the second loop fails.What you are looking for is the
ControlCountandControls[]properties instead. They access a visual control’s list of child controls instead – controls that have the parent control set as theirParentfor purposes of visual representation. OnlyTWinControl-derived controls (likeTPanel) can have child controls: