how to copy all the TLabels parented with a TPanel on delphi to another TPanel?

后端 未结 2 761
无人共我
无人共我 2021-01-25 21:01

I have a TPanel on a delphi form, I want to copy all the TLabels parented with this TPanel when i press a button and put them in other panel. Is there a way to do that? Thanks.

2条回答
  •  遥遥无期
    2021-01-25 21:09

    To copy the TLabel controls from one TPanel to another you can use something like this

    Procedure CopyLabels(ParentControl,DestControl:TWinControl);
    var
     i      : integer;
     ALabel : TLabel;
    begin
      for i := 0 to ParentControl.ControlCount - 1 do
       if ParentControl.Controls[i] is TLabel then
        begin
           ALabel:=TLabel.Create(DestControl);
           ALabel.Parent :=DestControl;
           ALabel.Left   :=ParentControl.Controls[i].Left;
           ALabel.Top    :=ParentControl.Controls[i].Top;
           ALabel.Width  :=ParentControl.Controls[i].Width;
           ALabel.Height :=ParentControl.Controls[i].Height;
           ALabel.Caption:=TLabel(ParentControl.Controls[i]).Caption;
           //you can add manually more properties here like font or another 
        end;
    end;
    

    and use like this

    CopyLabels(Panel1,Panel2);
    

    you can use the RTTI too, to copy the properties from a control to another, but as you does not specify your Delphi version only i show a simple example.

提交回复
热议问题