create Tframes on runtime:
Asked Answered
C

3

5

is it posible to create runtime frame and add existing panels like setting the parent of panel to the frame? and when it added, dulplicate the frame and use it?

like:

f:= Tframe. create(..)
...

panel3.parent = f; //where panel3 has many controls.

then duplicate the f? was it posible? how? or any other suggerstion? e

Clangor answered 9/12, 2010 at 13:53 Comment(0)
C
5

I don't think you would solve this by duplicating. What you need is a function like this:

function CreateFrameAndHostPanel(Owner: TComponent; Parent: TWinControl; Panel: TPanel): TFrame;
begin
  Result := TFrame.Create(Owner);
  Try
    Result.Parent := Parent;
    Panel.Parent := Result;
  Except
    FreeAndNil(Result);
    raise;  
  End;
end;
Cribbing answered 9/12, 2010 at 14:13 Comment(1)
@InTheName Because in this function we need to return a newly created object, and let the caller take responsibility for lifetime. But if there is an exception after creation then we need to destroy the object, before letting the the exception float up the call stack.Cribbing
P
3

You need to remember that all controls have a parent and an owner. Owners could be nil but then you need to free those controls through code, so most controls are owned by some other component.

Thus, if the owner gets destroyed, the panel would be destroyed too. And if the panel was created in design-time then it's owned by the form that it's on!

Destroying that form would destroy the panel!

But if you create the panels in runtime and set Application as owner instead of a form, they could be moved over multiple forms and frames.

But is it a good design pattern? I don't know what you're trying to do but it's likely a bad idea!

In general, it would be more practical to design the whole frame with panels in design-time. Then add some code that would allow the frame to be created by copying data from another panel or control. That would be a better design pattern...

Poi answered 10/12, 2010 at 8:45 Comment(0)
B
1

You must create the new frame (FRAME2) with the same code that you have used to create the first (FRAME1); And later, you must create all the component included (created on runtime) inside FRAME1 on FRAME2.

For to this, use:

for i := 0 to (FRAME1.ComponentCount - 1) do 
  ...
  cmp := TComponent(FRAME1.Component[i]);
  ... create cmp  on Frame2

You can try a second alternative; Save the FRAME1 using a TMemoryStream (SaveComponent) and later create the new Frame and retrieve the saved information on Stream (I don't have test this option).

Regards.

Bullivant answered 10/12, 2010 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.