如何仅使用父对象名称将父对象分配给对象

How to assign a parent to an object using only the parent object name

基本上,我有 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() returns 一个 TComponent,而 Parent 属性 期望一个 TWinControl。假设 FindComponent() 返回正确的组件,只需对其进行类型转换:

imgPanel.Parent := TWinControl(cPanel);