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

时间:2017-10-05 21:30:46

标签: delphi-7

基本上,我有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 

任何帮助或甚至其他方式来实现这一目标都会有所帮助。

1 个答案:

答案 0 :(得分:1)

FindComponent()返回TComponent,而Parent属性则需要TWinControl。假设FindComponent()正在返回正确的组件,只需键入它:

imgPanel.Parent := TWinControl(cPanel);