Image creation at runtime (SOLVED!) lazarus / 39-Graphics ---------------------------------------------------------------------- Middlecope · 2019-12-08 14:44 Can I show an image on a form, that was created at runtime? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interfaceuses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,  ExtCtrls; {ExtCtrls for TImage definition}type  { TForm1 }  TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);  private  public  end;var  Form1: TForm1;  Plaat : TImage;implementation{$R *.lfm}{ TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  Plaat:= TImage.Create(Form1);  Plaat.Color:= clWhite;  Plaat.Width:= 200; Plaat.Height:= 100;  Plaat.Top:= 10; Plaat.Left:= 30;  Plaat.Canvas.Pen.Width:= 3;  Plaat.Canvas.Font.Size:= 18;  Plaat.Canvas.TextOut(10,10,'Te lezen?');  Plaat.Visible:= TRUE;end;end.If I start the program the image doesn't show up. Any idea how to show the image on the Form???? Thanks for attention ---------------------------------------------------------------------- zeljko · 2019-12-08 14:57 You're missing Plaat.Parent := Form1; ---------------------------------------------------------------------- Middlecope · 2019-12-08 15:26 Thanks for the quick reply. Where should I put it in the program? ---------------------------------------------------------------------- winni · 2019-12-08 15:36 Hi! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);begin  Plaat:= TImage.Create(Form1);  Plaat.Parent := Form1; // <==== here  Plaat.Color:= clWhite;  Plaat.Width:= 200; Plaat.Height:= 100;  Plaat.Top:= 10; Plaat.Left:= 30;  Plaat.Canvas.Pen.Width:= 3;  Plaat.Canvas.Font.Size:= 18;  Plaat.Canvas.Brush.Color := clWhite; //<=====  Plaat.Canvas.FillRect(0,0,Plaat.Width, Plaat.height);  // <=======  Plaat.Canvas.TextOut(10,10,'Te lezen?');  Plaat.Visible:= TRUE;end;  And you should initialize the canvas with white (or whatever) color as show above. Winni ---------------------------------------------------------------------- Middlecope · 2019-12-08 20:10 Thanks for the advice but still nothing to see. I like to implement a lot of images on 1 form. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TYPETPlaat = ARRAY[1..10] OF TImage; The code in the first post was to find out the basics. Any suggestions how to do it are welcomeare welcome. ---------------------------------------------------------------------- wp · 2019-12-08 20:42 Quote from: Middlecope on December 08, 2019, 09:10:22 pmThanks for the advice but still nothing to see. winni's code is working perfectly (although I highly discourage using the form variable inside the form's code - better use "Self" instead of "Form1"). Is the FormCreate method assigned to the OnCreate event of the form? Quote from: Middlecope on December 08, 2019, 09:10:22 pmI like to implement a lot of images on 1 form. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TYPETPlaat = ARRAY[1..10] OF TImage; Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls; type   { TForm1 }   TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);  private    Plaat: array[1..10] of TImage;   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);const  W = 200;  H = 100;  DIST = 10;var  i: Integer;  x, y: Integer;begin  x := DIST;  y := DIST;  for i := 1 to 10 do  begin    Plaat[i] := TImage.Create(self);    Plaat[i].Parent := Self;    Plaat[i].Color:= clWhite;    Plaat[i].Width:= W;    Plaat[i].Height:= H;    Plaat[i].Top:= y;    Plaat[i].Left:= x;    Plaat[i].Canvas.Pen.Width:= 3;    Plaat[i].Canvas.Font.Size:= 18;    Plaat[i].Canvas.Brush.Color := clWhite;    Plaat[i].Canvas.FillRect(0,0,Plaat[i].Width, Plaat[i].Height);    Plaat[i].Canvas.TextOut(10,10,'Te lezen?');    x := x + W + DIST;    if x > 2 *(W+DIST) then  // two images per row    begin      y := y + H + DIST;      x := DIST;    end;  end;  Width := 3*DIST + 2*W;  Height := 6*DIST + 5*H;end; end. ---------------------------------------------------------------------- Middlecope · 2019-12-09 13:46 Thanks for all the work. I imported your unit and gave it a try Lazarus 1.8.2 FPC 3.04 on Linux (OpenSUSE Leap)  I see an empty form On WIN 10 I also only see an empty form How to debug? ---------------------------------------------------------------------- wp · 2019-12-09 16:17 Quote from: Middlecope on December 09, 2019, 02:46:20 pm[...] I imported your unit [...] What does this mean? I already asked: Did you assign the FormCreate event handler to the OnCreate event of the form? This means: after you pasted the code from the forum into the unit you must go to the object inspector, find the OnCreate event on page "Events" and click on the '...' button - then the code editor should jump to the FormCreate code, and "FormCreate" should appear in the object inspector next to "OnCreate" to indicate that the method is linked to the event - see screenshot. If this has not been made the FormCreate method is not called - you can check this by adding a breakpoint to any line of this procedure (click on the editor gutter of the line to get the breakpoint): program execution must stop at this line. Try also the attached complete project. It is working - if not, something is very wrong on your system. I checked it in Win 10 with Laz trunk/fpc 3.0.4 and Laz 1.8.4 / fpc 3.04, and in Linux Mint 18.1 with Laz trunk / fpc3.0.4. ---------------------------------------------------------------------- Middlecope · 2019-12-09 18:58 Yes That did the work. It works fine now. Thanks to all the hero members I can go on with my work Thanks Teunis