Free a TPanel within a btn click event handler lazarus / 31-LCL ---------------------------------------------------------------------- phoenix27 · 2023-08-20 05:16 At runtime i create a TPanel which contains a TscrollBox and a TButton to close the TPanel itself. Now, i get an abstract error when i close the TPanel within the TButton click event handler and i sort of understand it, because you can' t remove a button(whose owner is the TPanel) within its click event. A solution would be to make the TPanel a global variable or a field member of its parent and then create a timer in the click event which would remove it without errors. Is there another solution? ---------------------------------------------------------------------- phoenix27 · 2023-08-20 05:37 Another thing. If i create a TTimer at runtime in order to remove the TPanel, how do i free it after it has absolved its duty? I cannot do that in the OnTimer event handler, right? It's the same problem as with the TButton click event. Bahh ---------------------------------------------------------------------- phoenix27 · 2023-08-20 05:44 And another thing.(How many ahaha). In an event handler usually there is the sender parameter which you can cast to obtain the real control. How can i store the address of the real control, not the variable? For example: 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.ButtonOnClick(Sender: TObject);var   pnl: TPanel;begin   pnl := Sender as TPanel;   //let's suppose i have a Pointer ptr somewhere   ptr := @pnl; // wrong because after the procedure exits that pointer will point to garbage   //if i do   ptr := pnl; // i tried but it doesn't work, i thought pnl is a pointer under the hoodend; So how can i get the real address of the panel? Is there a way to get it and store it in a pointer variable? ---------------------------------------------------------------------- KodeZwerg · 2023-08-20 06:41 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, StdCtrls; type   TMyPanel = record    Panel: TPanel;    Button: TButton;    // and whatever else  end;  TMyPanels = array of TMyPanel;   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);  strict private    Panels: TMyPanels;  private    procedure AddPanel(const AOwner: TComponent; const AParent: TWinControl);    procedure PanelClick(Sender: TObject);  public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin  AddPanel(nil, Self);end; procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);var  i: Integer;begin  for i := High(Panels) downto Low(Panels) do    Panels[i].Panel.Free;end; procedure TForm1.AddPanel(const AOwner: TComponent; const AParent: TWinControl);var  i: Integer;begin  i := Length(Panels);  SetLength(Panels, Succ(i));  Panels[i].Panel := TPanel.Create(AOwner);  try    Panels[i].Panel.Parent := AParent;    Panels[i].Panel.Align := alTop;    Panels[i].Panel.Height := 25;    Panels[i].Panel.Tag := i;    Panels[i].Button := TButton.Create(Panels[i].Panel);    try      Panels[i].Button.Parent := Panels[i].Panel;      Panels[i].Button.Align := alRight;      Panels[i].Button.Caption := 'Close';      Panels[i].Button.Width := 80;      Panels[i].Button.OnClick := @PanelClick;      Panels[i].Button.Tag := i;    finally    end;  finally    Panels[i].Panel.Visible := True;  end;end; procedure TForm1.PanelClick(Sender: TObject);var  i: Integer;begin  if Sender is TButton then    begin      i := (Sender as TButton).Tag;      Panels[i].Panel.Free;      Delete(Panels, i, 1);    end;end; end. ---------------------------------------------------------------------- avk · 2023-08-20 08:42 This simpler version also seems to work: 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, StdCtrls, ExtCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  private    procedure TryFreeOwner(Sender: TObject);  public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.TryFreeOwner(Sender: TObject);begin  if (Sender is TComponent) and (TComponent(Sender).Owner <> nil) then    TComponent(Sender).Owner.Free  else    Sender.Free;end; procedure TForm1.Button1Click(Sender: TObject);var  Panel: TPanel;  Button: TButton;begin  Panel := TPanel.Create(Self);  Panel.Left := 5 + Random(10) * 2;  Panel.Top := 5 + Random(10) * 2;  Panel.Height := 100;  Panel.Width := 100;  Button := TButton.Create(Panel);  Button.Left := 10;  Button.Top := 10;  Button.Caption := 'Close';  Button.OnClick := @TryFreeOwner;  Button.Parent := Panel;  Panel.Parent := Self;end; end. ---------------------------------------------------------------------- Bart · 2023-08-20 11:08 Can't you use Application.ReleaseComponent(The_panel_in_question) for that? IIRC the freeing will be queued, so the button on the panel won't be destroyed insid it's own event. Bart ---------------------------------------------------------------------- BrunoK · 2023-08-20 12:45 A technique I used very long ago. ---------------------------------------------------------------------- jamie · 2023-08-20 15:02 Just post a user message to the form. When you handle that message, you can free the panel from there, Use POSTMESSAGE ---------------------------------------------------------------------- cdbc · 2023-08-20 16:34 Hi In your form, create this message-handler: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 uses ...,LMessages,LCLIntf;const  LMCLOSEPANEL = 1025; TForm1 = class(TForm)...private...public  procedure LMClosePanel(var Message: TLMessage); message LMCLOSEPANEL;...end; implementation ...procedure TForm1.LMClosePanel(var Message: TLMessage);begin  TPanel(Message.WParam).Free;end; Then in your TForm1.Button1Click method do this: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 begin  postmessage(Form1.Handle,LMCLOSEPANEL,ptrint(myPanel),0) ;end; Something like this I have used on occasion, it works. EDIT: What Jamie said Another option is Application.AsyncQueue... HTH Regards Benny ---------------------------------------------------------------------- lainz · 2023-08-20 16:42 Quote from: Bart on August 20, 2023, 01:08:32 pmCan't you use Application.ReleaseComponent(The_panel_in_question) for that? IIRC the freeing will be queued, so the button on the panel won't be destroyed insid it's own event. Bart +1 ---------------------------------------------------------------------- phoenix27 · 2023-08-21 04:26 Identifier idents no member "ReleaseComponent". Which unit should i include in order to use Application.ReleaseComponent? ---------------------------------------------------------------------- Bart · 2023-08-21 12:25 TApplication is in Forms unit. See https://lazarus- ccr.sourceforge.io/docs/lcl/forms/tapplication.releasecomponent.html about it's working mechanism. Bart ---------------------------------------------------------------------- phoenix27 · 2023-08-22 02:04 Thank you to everybody. Very interesting solutions. I had the unit 'forms' included of course(it's a basic unit after all and gets included automatically) but i couldn't find the method 'ReleaseComponent' and a while ago couldn't find the 'ProcessMessages' method and you know why? Because the unit 'forms' was at the beginning of the 'uses' section and surely the Application class was shadowed by another class with the same name in some other unit declared after it. I solved it by putting 'forms' at the very end.