假设我有一个包含面板(以及许多其他控件)的表单A和一个空表单B。
我能否通过编程的方式将面板从表单A中分离并移动到表单B中(然后可能再次移回表单A)?
我知道可以更改面板的所有者,但是否可以在不同的表单之间使用?
更新:
经过一些谷歌搜索,我发现有一个ParentWindow属性。
在考虑所有权时,否则表单A的销毁将导致您在表单B上的面板消失(即破坏),甚至更糟。
type
TForm2 = class(TForm)
public
InsertedPanel: TControl; // or TPanel
.
procedure RemoveComponents(AForm: TComponent; AControl: TWinControl);
var
I: Integer;
begin
for I := 0 to AControl.ControlCount - 1 do
begin
if AControl.Controls[I] is TWinControl then
RemoveComponents(AForm, TWinControl(AControl.Controls[I]));
if AControl.Controls[I].Owner = AForm then
AForm.RemoveComponent(AControl.Controls[I]);
end;
AForm.RemoveComponent(AControl);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Form2.InsertedPanel := Panel1;
Panel1.Parent := nil;
RemoveComponents(Self, Panel1);
Form2.InsertComponent(Form2.InsertedPanel); // < this is not necessary
Form2.InsertedPanel.Parent := Form2; // as long as Parent is set
Panel1 := nil; // or if you free the panel
end; // manually
Parent
设置为 FormB
:Panel1.Parent := FormB;
FormB
。
ParentWindow
不起作用。请参阅文档中的此处:"如果 Parent 不是 nil(Delphi)或 NULL(C++),则设置 ParentWindow 没有效果。"Parent
不是 nil(它是 formA)。 - Ken White