如何将delphi上以TPanel为父级的所有TLabels复制到另一个TPanel?

发布于 2024-10-27 13:38:55 字数 95 浏览 2 评论 0原文

我在 delphi 表单上有一个 TPanel,我想复制所有 TLabels 当我按下按钮并将它们放置在这个 TPanel 上时 在其他面板中。 有办法做到这一点吗? 谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最终幸福 2024-11-03 13:38:55

要将 TLabel 控件从一个 TPanel 复制到另一个,您可以使用类似的方法

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;

CopyLabels(Panel1,Panel2);

也可以使用 RTTI 将属性从一个控件复制到另一个,但由于您没有指定您的 Delphi 版本,所以我仅展示一个简单的示例。

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.

人生百味 2024-11-03 13:38:55

TPanel 是组件的容器。它的 Controls 属性中有一个子组件列表。您可以迭代此列表以访问其子项。

按下按钮时,您的代码必须

  1. 迭代Panel1的控件列表

  2. 检查该控件是否是TLabel

  3. 将 TLabel 的 Parent 属性更改为 Panel2

如下所示

for i := 0 to Panel1.ControlCount - 1 do
  if Panel1.Controls[i] is TLabel then
    (Panel1.Controls[i] as TLabel).Parent:=Panel2;

但是,等等!,这不起作用。为什么?因为“即时”进行此更改,您将更改正在迭代的同一个列表。

因此,您必须将要移动的标签保存在临时列表中。像这样的东西...

 var 
  i:integer;
  l:TObjectList;

 begin
  l:=TObjectList.Create;
  l.ownsObjects:=False;
  for i := 0 to Panel1.ControlCount - 1 do
   if Panel1.Controls[i] is TLabel then
     l.add(Panel1.Controls[i]);

  for i:= 0 to l.Count-1 do
    (l[i] as TLabel).Parent:=Panel2;

  l.Free;
 end;

TPanel is a container of Components. It has a list of its child components in its Controls property. You may iterate over this list to get access to its children.

At the press of the button your code has to

  1. iterate on the Controls list of Panel1

  2. check if the control is a TLabel

  3. change the Parent property of the TLabel to be Panel2

something like this

for i := 0 to Panel1.ControlCount - 1 do
  if Panel1.Controls[i] is TLabel then
    (Panel1.Controls[i] as TLabel).Parent:=Panel2;

But, wait!, this will not work. Why? Because doing this change 'on the fly' you will be changing the very same list you are iterating over.

So, you have to save the labels to be moved in a temporary list. Something like this...

 var 
  i:integer;
  l:TObjectList;

 begin
  l:=TObjectList.Create;
  l.ownsObjects:=False;
  for i := 0 to Panel1.ControlCount - 1 do
   if Panel1.Controls[i] is TLabel then
     l.add(Panel1.Controls[i]);

  for i:= 0 to l.Count-1 do
    (l[i] as TLabel).Parent:=Panel2;

  l.Free;
 end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文