TextOut on a TCustomPanel created at Run Time lazarus / 31-LCL ---------------------------------------------------------------------- Jimm · 2019-04-17 10:00 I must be missing something really simple here, but after two days of looking I can't find any solution  . I want to write text directly on the canvas of a TCustomPanel which has been created at run time: 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.Button1Click(Sender: TObject);var MyPanel: TCustomPanel;begin  MyPanel := TCustomPanel.Create (Self);  MyPanel.Parent := Self;  MyPanel.Name := 'MyPanel';  MyPanel.Caption := 'JimJam';  MyPanel.Canvas.TextOut(10, 10, 'Hello');   Panel1.Canvas.TextOut(10, 10, 'Hello');end; MyPanel is created, its caption becomes 'JimJam' but 'Hello' is not visible.  Panel1 which is a TPanel placed on the form at design time, shows its caption and the text 'Hello' correctly. What am I doing wrong ?  I get the same results in Windows and Linux Mint. ---------------------------------------------------------------------- wp · 2019-04-17 10:19 Don't paint in an OnClick event. When the OS decides to repaint the control or the form later your painting will be gone because the OnClick event does not fire again. Use the OnPaint event of the panel or your custompanel instead, and in the ButtonClick only request the control to be repainted: 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.Button1Click(Sender: TObject);var   MyPanel: TCustomPanel;begin  MyPanel := TCustomPanel.Create (Self);  MyPanel.Parent := Self;  MyPanel.Name := '';  // no name for run-time created controls!  MyPanel.Caption := 'JimJam';  MyPanel.OnPaint := @PanelPaint;  MyPanel.Invalidate;   // force MyPanel to redraw itself   Panel1.OnPaint := @PanelPaint;  // this could happen somewhere else  Panel1.Invalidate;   // force Panel1 to redraw itselfend;  procedure TForm1.PanelPaint(Sender: TObject);begin  with (Sender as TCustomPanel) do begin    // assuming Sender is a panel    Canvas.Brush.Color := Color;    Canvas.FillRect(0, 0, Width, Height);    TextOut(10, 10, 'Hello');  end;end; Another issue is that you assign a fixed name to the run-time created panel. This is not good, because when you click on Button1 a second time, a second panel will be created but get the same name - this will crash your program because the Name of a component must be unique. Better to leave the Name empty. Normally, the Name is only needed by the Object Inspector. ---------------------------------------------------------------------- Jimm · 2019-04-17 10:35 Thanks Hero, introducing OnPaint has solved the problem. Jim ---------------------------------------------------------------------- howardpc · 2019-04-17 10:55 Another approach would be to write your own custom panel control, with properties designed to let you place text where desired. 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, Forms, Controls, Graphics, ExtCtrls, Themes, StdCtrls; type   { TMyPanel }   TMyPanel = class(TCustomPanel)  private    FTextLeft: Integer;    FTextTop: Integer;  protected    procedure Paint; override;  public    property TextLeft: Integer read FTextLeft write FTextLeft;    property TextTop: Integer read FTextTop write FTextTop;  end;   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  private    FMyPanel: TMyPanel;  end; var  Form1: TForm1; implementation uses  LCLIntf; {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  FMyPanel := TMyPanel.Create(Self);  with FMyPanel do  begin      SetBounds(20, 20, 200, 100);      Parent := Self;    end;end; procedure TForm1.Button1Click(Sender: TObject);begin  FMyPanel.TextLeft := 10;  FMyPanel.TextTop := 10;  FMyPanel.Caption := 'Hello';end; { TMyPanel } procedure TMyPanel.Paint;var  r: TRect;   procedure PaintBevel(aBevel: TPanelBevel);  begin    case aBevel of    bvNone: ;      else        case BevelColor of          clDefault: Canvas.Frame3d(r, BevelWidth, aBevel);          else Canvas.Frame3d(r, BevelColor, BevelColor, BevelWidth);        end;    end;  end; begin  r := GetClientRect;   PaintBevel(BevelOuter);  InflateRect(r, -BorderWidth, -BorderWidth);  PaintBevel(BevelInner);   if Length(Caption) > 0 then  begin    Canvas.Brush.Color := Color;    case Enabled of      True: Canvas.Font.Color := Font.Color;      False: if ThemeServices.ThemesEnabled then               Canvas.Font.Color := clGrayText             else               begin                 Canvas.Font.Color := clBtnHighlight;                 OffsetRect(r, 1, 1);                 Canvas.TextOut(r.Left+FTextLeft, r.Top+FTextTop, Caption);                 Canvas.Font.Color := clBtnShadow;                 OffsetRect(r, -1, -1);               end;    end;     Canvas.TextOut(r.Left+FTextLeft, r.Top+FTextTop, Caption);  end;end; end.