Access the components of the children of a TObject

4

Ohayou Developers, I would like to know how to access the children of an object, for example. I have a TRectangle and a TLabel with your child. I would need to access the TLabel to change, for example, your text, color etc. What would be the command for this ?, also consider that the child can be another type of object, so I put Tobject in the title. Thank you.

    
asked by anonymous 08.11.2017 / 19:55

2 answers

4

When you talk about the object's "child", there are two possible interpretations: 1) The object is a TComponent and Owner of your children, which can be accessed by the Components property. For example:

for i := 0 to Form1.ComponentCount - 1 do
  if Form1.Components[i] is TLabel then
    TLabel(Form1.Components[i]).Caption := IntToStr(i);

2) The object is a TControl and Parent of your children, which can be accessed by the Controls property. For example:

for i := 0 to Panel1.ControlCount - 1 do
  if Panel1.Controls[i] is TLable then
    TLabel(Panel1.Controls[i]).Caption := IntToStr(i);

Generally the Form is the Owner of all components and visual controls that contain other controls (when the parent moves the children go together) are the Parents of their children.

    
10.11.2017 / 11:57
2

You need to access the component's Parent first with the components within it to gain access. I made an example to get access to TLabel .

I hope it helps.

var
  oLabel: TObject
begin
  oLabel := FindComponent('nome do TRectangle').FindComponent('nome do TLabel');
  if Assigned(oLabel) then
    TLabel(oLabel).TextSettings.FontColor := TAlphaColorRec.White;

end;

This example will work if the TLabel is within the TRectangle component, now if for example there is a TRectagle component within TToolBar and then TToolBar exists within TLabel . You would have to only add a FindComponent () by looking for TToolBar .

oLabel := FindComponent('nome do TRectangle').FindComponent('nome do TToolBar').FindComponent('nome do TLabel');
    
08.11.2017 / 20:19