Search code examples
delphidelphi-7timage

Delphi: creating & displaying TImage on FormCreate


Being quite a newbie, I've just had my first question answered (Delphi: TImage.Create causes Access violation) to immediately bump into a new problem:

procedure TSelectorForm.FormCreate(Sender: TObject);
var
  Loop: Byte;
begin
  for Loop := 1 to 10 do
  begin
    ArrayOfImages[Loop] := TImage.Create(SelectorForm);
    MainForm.MyImageList.GetBitmap(Loop - 1, ArrayOfImages[Loop].Picture.Bitmap);
    ArrayOfImages[Loop].Top := ...
    ArrayOfImages[Loop].Left := ...
    ArrayOfImages[Loop].Enabled := True;
    ArrayOfImages[Loop].Visible := True;
  end;
end;

When I display this form

procedure TMainForm.MyImageClick(Sender: TObject);
begin
  SelectorForm.Visible := True;
end;

the images aren't visible. What am I doing wrong?

I want to thank everyone for their advice. Hopefully, asking elementary questions helps others avoid asking them in future :-)


Solution

  • Set the Parent property of all image components to the form that contains them.

    procedure TSelectorForm.FormCreate(Sender: TObject);
    var
      Loop: Byte;
    begin
      for Loop := 1 to 10 do
      begin
        ArrayOfImages[Loop] := TImage.Create(SelectorForm);
        MainForm.MyImageList.GetBitmap(Loop - 1, ArrayOfImages[Loop].Picture.Bitmap);
        ArrayOfImages[Loop].Top := ...
        ArrayOfImages[Loop].Left := ...
        ArrayOfImages[Loop].Visible := True;
        ArrayOfImages[Loop].Parent := SelectorForm;
      end;
    end;