Forum > Graphics
Run-time TPanel move
davor:
Is it possible to move same TPanel component between different forms in run-time?
Handoko:
Hello davor,
Welcome to the forum.
Yes, most visual components can be moved to other forms easily.
Try this:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
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:
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:
--- Quote from: Handoko on November 20, 2023, 03:59:47 pm ---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.
--- End quote ---
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.
Navigation
[0] Message Index
[#] Next page