Access the child components of a Tobject

Asked

Viewed 757 times

4

Ohayou Developers, I’d like to know how you access the children of an object, for example. I have a Trectangle and a Tlabel with your child. I would need to access 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.

2 answers

4

When speaking of the object’s "son", 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 the visual controls that contain other controls (when moving the parent the children go together) are the parents of their children.

2

You need to access the Father of the component first accompanied by the components inside it to have access. I made an example to have 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 inside the component TRectangle, now if for example there exists within a TRectagle a component TToolBar and then inside the TToolBar exist the TLabel. You would only have to add a Findcomponent() seeking the TToolBar.

oLabel := FindComponent('nome do TRectangle').FindComponent('nome do TToolBar').FindComponent('nome do TLabel');
  • 1

    +1, and if you know the name of Trectangle, ie if it is a visible object in the design, you can take the shortcut: NomeRectangle.FindComponent('nome do TLabel');

  • @Júniormoreira well remembered

  • Wouldn’t you be able to do the direct access without having to search? for example Trectangle.Children[0]." Sticky Component"(Tlabel). Color etc etc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.