Run-time TPanel move lazarus / 39-Graphics ---------------------------------------------------------------------- davor · 2023-11-20 14:18 Is it possible to move same TPanel component between different forms in run-time? ---------------------------------------------------------------------- Handoko · 2023-11-20 14:42 Hello davor, Welcome to the forum. Yes, most visual components can be moved to other forms easily. Try 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 unit frmMain; {$mode objfpc}{$H+} interface uses  Classes, Forms, Controls, Graphics, ExtCtrls, StdCtrls, frmChild; type   { TMainform }   TMainform = class(TForm)    Button1: TButton;    Panel1: TPanel;    procedure Button1Click(Sender: TObject);    procedure FormActivate(Sender: TObject);    procedure FormCreate(Sender: TObject);  end; var  Mainform: TMainform; implementation {$R *.lfm} { TMainform } procedure TMainform.FormCreate(Sender: TObject);begin  Panel1.BevelInner := bvLowered;  Panel1.Color      := clCream;end; procedure TMainform.FormActivate(Sender: TObject);begin  Childform.Left := Left + Width + 10;  Childform.Top  := Top;  Childform.Show;end; procedure TMainform.Button1Click(Sender: TObject);begin  if Panel1.Parent = Mainform then  begin    Panel1.Parent := Childform;    Exit;  end;  if Panel1.Parent = Childform then    Panel1.Parent := Mainform;end; end. ---------------------------------------------------------------------- wp · 2023-11-20 14:56 Hmmm, I don't know... A control also has an owner - this is the component which is responsible to destroy it. The panel was created by Form1 and, therefore, has Form1 as its owner. When the panel now is moved to Form2 with your code still Form1 remains the owner. When now Form1 is destroyed before Form2 the panel is removed from Form2 (hopefully!) - but this will be very surprising for the user. The problem is that the Owner is a read-only property and cannot be change easily in the same way as you did with the Parent. One solution might be to create a new panel on Form2, assign the Form1.panel to it (i.e. copy its properties), and then destroy the panel on Form1. Of course, this must be repeated for all controls sitting on the panel... But possibly there is a better solution... ---------------------------------------------------------------------- Handoko · 2023-11-20 14:59 I already thought about it before posting the reply. I am sure no problem with my code, because Form1 is the main form. Closing main form will cause all other forms to close too. And you can see from the code, closing child form will cause the program to halt. ---------------------------------------------------------------------- wp · 2023-11-20 15:55 Quote from: Handoko on November 20, 2023, 03:59:47 pmI already thought about it before posting the reply. I am sure no problem with my code, because Form1 is the main form. Closing main form will cause all other forms to close too. And you can see from the code, closing child form will cause the program to halt. But why should closing the child form close the application? You put a "Halt" into the code. But why? When the child from does NOT close the application, but is destroyed and reactivates the main form, we have another confusing situation: The panel was parented by the child form. When the child form is killed the panel disappears from the screen along with its parent, but still exists - without parent - in memory, but still owned by the main form. This does not mean that it must be a dangerous situation, but it least it is unusual. ---------------------------------------------------------------------- Handoko · 2023-11-20 16:03 Quote from: wp on November 20, 2023, 04:55:37 pmBut why should closing the child form close the application? You put a "Halt" into the code. But why? If you download the code, remove the halt and run it then you will know why. Quote from: wp on November 20, 2023, 04:55:37 pmWhen the child from does NOT close the application, but is destroyed and reactivates the main form Closing the child form does not cause it to be freed or destroyed. It simply hides the form. That is the default form behavior (in Lazarus). ---------------------------------------------------------------------- luca · 2023-11-20 16:30 Hi, I've downloaded the code. The Button1Click event is never fired. Every time I click on the button the FormActivate event is triggered and nothing more happen. If I removed the code from FormActivate procedure, and show the Child form when I click on another button (I've created on main form), then all it's fine and no error appears even if I remove the Halt instruction. It seems the problem is that every time you click on the MainForm the ChildForm is showed back and the program can't exit. Best regards Luca ---------------------------------------------------------------------- wp · 2023-11-20 17:20 Showing the child from in Handoko's code in the OnActivate handler should be protected by some state variable preventing multiple execution: 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 type  TMainForm = class(TForm)  ...  private    Activated: Boolean;  end; procedure TMainform.FormActivate(Sender: TObject);begin  if not Activated then  begin    Activated := true;    Childform.Left := Left + Width + 10;    Childform.Top  := Top;    Childform.Show;  end;end; ---------------------------------------------------------------------- luca · 2023-11-20 18:48 Generally speaking I don't like to use OnActivate event. Regards Luca ---------------------------------------------------------------------- davor · 2023-11-21 09:21 Big Thanks Handoko!!!! This is exactly the solution I was looking for. ---------------------------------------------------------------------- Handoko · 2023-11-21 10:54 Quote from: davor on November 21, 2023, 10:21:47 amThis is exactly the solution I was looking for. Glad to know it is useful. But you should pay attention about the potential issue that wp pointed out. That would be too advanced for you now, just remember if you ever have problem with this 'moving component to other form', report back. Quote from: luca on November 20, 2023, 07:48:07 pmGenerally speaking I don't like to use OnActivate event. As a programmer, I would try and use anything that works. Is there any reason you don't like it? Quote from: luca on November 20, 2023, 05:30:08 pmI've downloaded the code. The Button1Click event is never fired. Every time I click on the button the FormActivate event is triggered and nothing more happen. I'm sorry it does not work for you. I wrote it and tested only on Linux. Did you use Windows? Quote from: wp on November 20, 2023, 06:20:33 pmShowing the child from in Handoko's code in the OnActivate handler should be protected by some state variable preventing multiple execution: Thank you for the improvement. I like to use global variable as little as possible, I would suggest to move that private/global variable to local: 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 TMainform.FormActivate(Sender: TObject);const  Activated: Boolean = false;begin  if not Activated then  begin    Activated := true;    Childform.Left := Left + Width + 10;    Childform.Top  := Top;    Childform.Show;  end;end;