Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :-)

like image 839
Mikhail Avatar asked Dec 13 '22 03:12

Mikhail


2 Answers

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;
like image 130
mghie Avatar answered Dec 26 '22 13:12

mghie


Well, I suppose that you have to add the single TImage components to the control-list of the current form?

That means: just because you're creating a TImage in code does not mean that it is actually added to the form's controls. You'll have to do that in code - however, I've not touched Delphi in a couple of years, so I can't provide any code right now.

like image 25
Thorsten Dittmar Avatar answered Dec 26 '22 15:12

Thorsten Dittmar