0

I'd like to swap the subforms that appear in a container on my main form.

I've found a method of assigning one form as a subform of another here by making it's parent a container on the main form.

procedure TParentForm.EmbeddForm(AParent:TControl; AForm:TCustomForm);
begin
    while AForm.ChildrenCount>0 do
        AForm.Children[0].Parent:=AParent;
end;

This works pretty much as I expected it to for adding a subform; however, I can't seem to replace it once it's already been made a subform.

niling the parent for the subform doesn't seem to do the trick, nor does setting its parent to it self.

Is there a way to un-embed this subform from its container?

I'd rather not destroy an recreate these subforms all the time, just swap them out.

Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
BIBD
  • 15,107
  • 25
  • 85
  • 137
  • 1
    You are not re-parenting the sub-form. You are moving its children to be children of another form. So you undo that by restoring their original parent. You need to keep track of each control that you re-parented, and then set the parent of each of those controls back to the original form. The entire approach seems odd though. Wouldn't a frame be better? Or perhaps reparenting the entire form. – David Heffernan Nov 16 '15 at 22:33

1 Answers1

1

In Firemonkey, to embed a form, you should first encapsulate all your controls on a TLayout of some sort. Then, you can assign the parent of that layout to whatever container you need.

MyLayout.Parent := MyContainerInAnotherForm;

This way, you only have one variable (of the layout) to reference the entire "form". Bear in mind that FMX is widely based around layouts.

When you want to re-assign the original form, just assign that layout's parent back to its original container.

MyLayout.Parent := MyOriginalContainerForm;
Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327