TPaintBox not cleared lazarus / 31-LCL ---------------------------------------------------------------------- segfault · 2017-10-12 12:15 I'm writing a list of characters of fixed length to a TPaintBox (when the list length exceeds 10, the first character is removed). Every time one of 2 buttons is clicked the character on the button is drawn on the canvas, which should be cleared prior to drawing the current list. Everything works ok but the canvas isn't being cleared which means that the previous list remains. Here's the code: 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, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,  ExtCtrls; type   { TForm1 }   TForm1 = class(TForm)    banker: TButton;    player: TButton;    marquee: TPaintBox;    procedure bankerClick(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure playerClick(Sender: TObject);  private    { private declarations }  public    { public declarations }    procedure update_marq;  end; var  Form1: TForm1;  marq_list : string;implementation {$R *.lfm} { TForm1 }procedure TForm1.update_marq;var  xpos, ypos, i : integer;begin  marquee.canvas.Clear;  xpos := 0;  ypos := 0;  marquee.canvas.brush.style := bsclear;  for i := 1 to length(marq_list) do begin    if marq_list[i] = '1' then      marquee.canvas.textout(xpos,ypos,'B')    else      marquee.canvas.textout(xpos,ypos,'    P');    inc(ypos, 12)  end;end; procedure TForm1.bankerClick(Sender: TObject);begin  marq_list := marq_list + '1';  if length(marq_list) > 10 then delete(marq_list,1,1);  update_marq;end; procedure TForm1.FormCreate(Sender: TObject);begin  marq_list := '';end; procedure TForm1.playerClick(Sender: TObject);begin  marq_list := marq_list + '0';  if length(marq_list) > 10 then delete(marq_list,1,1);  update_marq;end; end.  screenshot here: https://s1.postimg.org/5d52u155xb/snapshot1.png          Thanks for any help. ---------------------------------------------------------------------- wp · 2017-10-12 12:32 A paintbox does not "remember" what you painted in an OnClick event. How could it? It has nothing to store that information. It does have an OnPaint event, though, and this is where you must put your painting code. The OnClick should only contain a call to Paintbox.Invalidate or Paintbox.Refresh ("Invalidate" preferred because it elimates multiple requests for repainting). Untested... 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.MarqueePaint(Sender: TObject);// instead of "Update_marq", ... or call Update_marq herevar  xpos, ypos, i : integer;begin  marquee.canvas.Clear;  xpos := 0;  ypos := 0;  marquee.canvas.brush.style := bsclear;  for i := 1 to length(marq_list) do begin    if marq_list[i] = '1' then      marquee.canvas.textout(xpos,ypos,'B')    else      marquee.canvas.textout(xpos,ypos,'    P');    inc(ypos, 12)  end;end; procedure TForm1.bankerClick(Sender: TObject); begin  marq_list := marq_list + '1';  if length(marq_list) > 10 then delete(marq_list,1,1);  Marquee.Invalidate;  // Request to repaint the paintboxend; procedure TForm1.playerClick(Sender: TObject);begin  marq_list := marq_list + '0';  if length(marq_list) > 10 then delete(marq_list,1,1);  Marquee.Invalidate;  // Request to repaint the paintboxend; ---------------------------------------------------------------------- segfault · 2017-10-12 13:17 Thanks, your code works fine. Quote from: wp on October 12, 2017, 02:32:00 pmA paintbox does not "remember" what you painted in an OnClick event. How could it? It has nothing to store that information. I find this confusing... why should 'remembering' anything even be relevant if the paintbox is cleared prior to drawing on it? The problem appeared to be that the paintbox was remembering which is why the previous list remained and didn't seem to be cleared even though I had used 'marquee.canvas.Clear'. But anyway your comment tells me that I don't really understand how objects and the GUI model works at all. Up until now I've only written procedural programs and have been avoiding OOP. It seems I can't avoid it any longer (sigh). Quite frankly, it does my head in. There are lots of tutorials around but they are either very terse and assume too much or else they are too simple. Would it help to read some old Delphi books? Lazarus is based on Delphi 7 right? I'm finding it all a bit overwhelming. ---------------------------------------------------------------------- wp · 2017-10-12 15:04 Quote from: segfault on October 12, 2017, 03:17:11 pmI find this confusing... why should 'remembering' anything even be relevant if the paintbox is cleared prior to drawing on it? The problem appeared to be that the paintbox was remembering which is why the previous list remained and didn't seem to be cleared even though I had used 'marquee.canvas.Clear'. What I wanted to say is: You never should put any drawing code into any event except for OnPaint. Suppose your OnClick handler fills the background of the paintbox in red color, nothing else, just for simplicity. When the user clicks the button the paintbox will be red - fine. But now the user decides to resize the form. When this happens Windows requests your program to repaint itself again to adapt to the new size. But the paintbox does not "know" how to repaint itself - it had been painted in the button's OnClick event, but this is long gone; therefore the paintbox is erased (this is the standard behavior built into the paintbox). If, on the other hand, you had put the painting code into the OnPaint event of the paintbox it would be called automatically, and the paintbox would be red again. Just look at the attached demo. Quote from: segfault on October 12, 2017, 03:17:11 pmWould it help to read some old Delphi books? Lazarus is based on Delphi 7 right?Lazarus is not "based" on Delphi 7, but it gets close. Although there may be subtle differences here and there, a well-written Delphi book will help you more than a poorly-written Lazarus article somewhere in the net. (Of course I don't want to say that all Lazarus articles are poorly written). ---------------------------------------------------------------------- segfault · 2017-10-12 17:54 Thanks for the nice demo which makes the point nicely. However I'm still a little puzzled why my original code appeared to work ok but didn't clear the canvas in spite of the canvas.clear statement. Just trying to understand.  Quote from: wp on October 12, 2017, 05:04:11 pmAlthough there may be subtle differences here and there, a well-written Delphi book will help you more than a poorly-written Lazarus article somewhere in the net. (Of course I don't want to say that all Lazarus articles are poorly written). Not complaining about the Lazarus/FP documentation which is good and comprehensive, but I'm surprised there isn't at least one book aimed at beginners. According to the wiki there is 'Lazarus : The Complete Guide', but the link is dead. I also found this thread but no news of when there will be a second edition or replacement- http://forum.lazarus.freepascal.org/index.php/topic,34613.msg227166.ht ml#msg227166 Judging by the number of views of that thread there appears to be a demand for such a book... ---------------------------------------------------------------------- wp · 2017-10-12 17:59 Quote from: segfault on October 12, 2017, 07:54:11 pmHowever I'm still a little puzzled why my original code appeared to work ok but didn't clear the canvas in spite of the canvas.clear statement. Just trying to understand. Me too. Could you put that code into a little project to upload here? (Please only -pas, .lfm, .lpi and .lpr files packed into a common zip which you can upload from "Attachments and other options" below the edit field here). ---------------------------------------------------------------------- segfault · 2017-10-13 08:17 Ok, attached. Also I notice that although the program seems to work (apart from the canvas not being cleared), the marquee (paintbox) should be white but it's not. ---------------------------------------------------------------------- wp · 2017-10-13 09:14 All these observations are a consequence of the fact that you don't provide an OnPaint event handler. Call update_marq from the the OnPaint handler (double-click on "OnPaint" of the paintbox), replace the call the update_marq in the buttons' OnClick by marquee.Invalidate, and everything will work as expected: 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.bankerClick(Sender: TObject);begin  marq_list := marq_list + '1';  if length(marq_list) > 10 then delete(marq_list,1,1);  marquee.Invalidate;  // replaces: "update_marq"end; procedure TForm1.FormCreate(Sender: TObject);begin  marq_list := '';end; procedure TForm1.marqueePaint(Sender: TObject);  // newbegin  update_marq;end; procedure TForm1.playerClick(Sender: TObject);begin  marq_list := marq_list + '0';  if length(marq_list) > 10 then delete(marq_list,1,1);  marquee.Invalidate;  // replaces "update_marq"end; procedure TForm1.update_marq;var  xpos, ypos, i : integer;begin  marquee.canvas.Clear;  xpos := 0;  ypos := 0;  marquee.canvas.brush.style := bsclear;  for i := 1 to length(marq_list) do begin    if marq_list[i] = '1' then begin      marquee.canvas.textout(xpos,ypos,'B');    end    else      marquee.canvas.textout(xpos,ypos,'    P');    inc(ypos, 12)  end;end; ---------------------------------------------------------------------- jamie · 2017-10-14 01:18 I think what the issue here is the fact that the surface isn't getting cleaned from the prior paint operation. this will cause different characters to stack on top of each other unless a full repaint is done which requires a page to be erased.  A Tbitmap should be used to build the image in the background the same way it is being done now, but only clear the surface first using FillREct prior to doing the surface drawing. Then in the Onpaint handler of the PaintBox, simply need to canvas.Draw it on there.  Of course this could be over their head at this time.