Jump to content
Sign in to follow this  
Czar

find component within another component

Recommended Posts

There have been a few threads about this topic but I couldn't get any of them to work with my current challenge.

 

My challenge was accessing a label (child) on a panel. The problem I was having was GetChildCount and GetChildObject are not accessible from the objects so I cast them a Tw3TagContainer instead, probably ok to cast as something else. I will post my solution as an example. 

procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components

  var W3Panel1 := tw3panel.create(self);
  W3Panel1.setbounds(10,10,400,400);

  var mylbl := TW3Label.create(W3Panel1);
  mylbl.SetBounds(10,10,80,30);
  mylbl.caption := 'label on panel';
  mylbl.tagvalue := 2;


  for var i := TW3TagContainer(W3Panel1).GetChildCount -1 downto 0 do
  begin
   var obj := TW3TagContainer(W3Panel1).GetChildObject(i);
   if obj is TW3Label then
          tw3label(obj).caption := 'IT WORKED';
  end;
end;

 

 

 

Share this post


Link to post
Share on other sites

You can use ForEach() to loop through the children:

  MyPanel.ForEach( function (const Child: TW3TagContainer): TEnumResult
    begin
      if Child is TW3Label then begin
        TW3Label(Child).Caption:='IT WORKED!';
        //result:=erBreak; //If you want to exit
      end;
    end);

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×