Components vs Children in FireMonkey

Posted by on in Blogs
You have probably iterated through the Components array many many times as a Delphi developer.

Consider this simple application:



It's probably no surprise that listing the Components of the form like this...


procedure TForm2.Button1Click(Sender: TObject);
var
i: Integer;
begin
Memo2.Lines.Clear;
for i := 0 to ComponentCount-1 do
Memo1.Lines.Add(Components[i].ClassName+' (Name = "'+Components[i].Name+'")')
end;


...gives you the following output, which is very similar to the Structure View in the IDE, except that it doesn't show the relationships between the components.



Listing the children, and the children of all children (recursively) is simply done by using this procedure:


procedure TForm2.ListChildren(Obj : TFMXObject; Level : Integer);
var
i: Integer;
begin
for i := 0 to Obj.ChildrenCount-1 do begin
Memo2.Lines.Add(StringOfChar(' ',2*Level)+
Obj.Children[i].ClassName+
' (Name = "'+
Obj.Children[i].Name+
'")');
ListChildren(Obj.Children[i],Level+1);
end;
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
Memo2.Lines.Clear;
ListChildren(Self,0);
end;


The above code will give us a tree output with indentation to show us the children, grandchildren, etc, like this:



The list of children is much longer than you would possibly except at first. We see for instance that in FireMonkey a TButton consists of a TLayout, a TSubImage and a TText. The TSubImage and the TText in turn have 4 animations each to handle hover-in/hover-out and pressing/releasing. Likewise the TMemo holds scrollbars, a popup context menu, and much more.

The entire output from ListChildren follows below:
TSubImage (Name = "")
TButton (Name = "Button1")
TLayout (Name = "")
TSubImage (Name = "")
TRectAnimation (Name = "")
TRectAnimation (Name = "")
TRectAnimation (Name = "")
TRectAnimation (Name = "")
TText (Name = "")
TColorAnimation (Name = "")
TColorAnimation (Name = "")
TColorAnimation (Name = "")
TColorAnimation (Name = "")
TMemo (Name = "Memo1")
TLayout (Name = "")
TSubImage (Name = "")
TRectAnimation (Name = "")
TLayout (Name = "")
TSmallScrollBar (Name = "")
TSmallScrollBar (Name = "")
TScrollBar (Name = "")
TScrollBar (Name = "")
TBrushObject (Name = "")
TBrushObject (Name = "")
TFontObject (Name = "")
TScrollContent (Name = "")
TPopupMenu (Name = "")
TPopupMenuContent (Name = "")
TMenuItem (Name = "")
TMenuItemContent (Name = "")
TMenuItem (Name = "")
TMenuItemContent (Name = "")
TMenuItem (Name = "")
TMenuItemContent (Name = "")
TMenuItem (Name = "")
TMenuItemContent (Name = "")
TMenuItem (Name = "")
TMenuItemContent (Name = "")
TMenuItem (Name = "")
TMenuItemContent (Name = "")
TEdit (Name = "Edit2")
TLayout (Name = "")
TSubImage (Name = "")
TRectAnimation (Name = "")
TLayout (Name = "")
TLayout (Name = "")
TBrushObject (Name = "")
TBrushObject (Name = "")
TFontObject (Name = "")
TPopupMenu (Name = "")
TPopupMenuContent (Name = "")
TMenuItem (Name = "")
TMenuItemContent (Name = "")
TMenuItem (Name = "")
TMenuItemContent (Name = "")
TMenuItem (Name = "")
TMenuItemContent (Name = "")
TMenuItem (Name = "")
TMenuItemContent (Name = "")
TMenuItem (Name = "")
TMenuItemContent (Name = "")
TMenuItem (Name = "")
TMenuItemContent (Name = "")
TContentEdit (Name = "")
TClearEditButton (Name = "ClearEditButton1")
TLayout (Name = "")
TSubImage (Name = "")
TRectAnimation (Name = "")
TRectAnimation (Name = "")
TRectAnimation (Name = "")
TSubImage (Name = "")
TRectAnimation (Name = "")
TRectAnimation (Name = "")
TRectAnimation (Name = "")
TButton (Name = "Button2")
TLayout (Name = "")
TSubImage (Name = "")
TRectAnimation (Name = "")
TRectAnimation (Name = "")
TRectAnimation (Name = "")
TRectAnimation (Name = "")
TText (Name = "")
TColorAnimation (Name = "")
TColorAnimation (Name = "")
TColorAnimation (Name = "")
TColorAnimation (Name = "")
TViewport3D (Name = "Viewport3D1")
TDummy (Name = "")
TDummy (Name = "")
TCamera (Name = "")
TSphere (Name = "Sphere1")
TCube (Name = "Cube1")
TTextureMaterialSource (Name = "TextureMaterialSource1")

Enjoy!

Comments

  • Guest
    Olecramoak Thursday, 06 December 2012

    Cool! Been using similar code, but less optimized.

    BTW Anders, any news on mobile studio?

  • Please login first in order for you to submit comments
  • Page :
  • 1