开发者

create Tframes on runtime:

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


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;


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...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜