1. Home
  2. Computing & Technology
  3. Delphi Programming

How to Clone a Delphi Form

By Zarko Gajic, About.com

Creating Delphi objects (forms, components, etc) dynamically, at run-time, is a "standard" coding technique for Delphi developers. Almost every Form that you want to show modally will be created dynamically, displayed to the user and freed from the memory.

Besides instantiating Delphi Forms dynamically, you can decide to create a *clone* of an existing form - by using the TMemoryStream class.

Note: when you create a new clone from an existing form, the clone's OnCreate will not execute.

Here's how:

procedure FormClone(form : TForm) ;
var
   ms : TMemoryStream;
   clone : TForm;
begin
   ms := TMemoryStream.Create;
   try
     ms.WriteComponent(form) ;
     ms.Position := 0;
     clone := TFormClass(form.ClassType).CreateNew(Application) ;
     ms.ReadComponent(clone) ;

     clone.Left := form.Left + 10;
     clone.Top := form.Top + 10;
     clone.Show;
   finally
     ms.Free;
   end;
end;

//Usage
procedure TForm1.Button1Click(Sender: TObject) ;
begin
   FormClone(Form1) ;
end;

Delphi tips navigator:
» How to Remove the "Today" Mark from the TDateTimePicker
« How to Change the Colors of the TProgressBar

More Delphi Programming Quick Tips
Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2006 Tips
  7. How to Clone a Delphi Form

©2009 About.com, a part of The New York Times Company.

All rights reserved.