如何将一个父对象分配给仅使用父对象名称的对象
问题描述:
基本上,我有8个面板,我想通过使用循环为其中的所有图片分配一个图片,以便使用TComponent变量和图片我在运行时创建的。但我无法找到一种方法来使用此字符串('pnlDisplay'+ inttostr(i))将该图像分配给父级。 所以我的代码看起来是这样的:如何将一个父对象分配给仅使用父对象名称的对象
var
imgPanel : TImage;
cPanel : TComponent;
begin
for i := 1 to 8 do
begin
cPanel := FindComponent('pnlDisplay' + inttostr(i));
imgPanel := TImage.Create(cPanel);
imgPanel.Parent := cPanel; //Here is my problem
imgPanel.Picture.LoadFromFile('Pic' + inttostr(i) + '.jpg');
imgPanel.Visible := True;
end;
end
任何帮助,甚至另一种方式来实现,这将是有益的。
答
FindComponent()
返回TComponent
,而Parent
财产预计TWinControl
代替。假设FindComponent()
正在返回正确的组件,只需输入它:
imgPanel.Parent := TWinControl(cPanel);
非常感谢!它现在有效 –