Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can propably use the <a href="https://stackoverflow.com/questions/120858/replace-visual-component-at-runtime-in-delphi-win32#122915">CLoneProperties routine from the answer</a> to "<a href="https://stackoverflow.com/questions/120858/replace-visual-component-at-runtime-in-delphi-win32">Replace visual component at runtime</a>", after you have created the dup components in a loop thru the parent's controls. </p> <p><strong>Update</strong>: some working code.... </p> <p>. I assume from your question that you want to duplicate the Controls that are contained in a WinControl (as a Parent is a TWinControl).<br> . As I did not know if you also wanted to hook the duplicated controls with the same Event Handlers as the originals, I made an option for that.<br> . And you may want to give a proper meaningful Name to the duplicated controls. </p> <pre><code>uses TypInfo; procedure CloneProperties(const Source: TControl; const Dest: TControl); var ms: TMemoryStream; OldName: string; begin OldName := Source.Name; Source.Name := ''; // needed to avoid Name collision try ms := TMemoryStream.Create; try ms.WriteComponent(Source); ms.Position := 0; ms.ReadComponent(Dest); finally ms.Free; end; finally Source.Name := OldName; end; end; procedure CloneEvents(Source, Dest: TControl); var I: Integer; PropList: TPropList; begin for I := 0 to GetPropList(Source.ClassInfo, [tkMethod], @PropList) - 1 do SetMethodProp(Dest, PropList[I], GetMethodProp(Source, PropList[I])); end; procedure DuplicateChildren(const ParentSource: TWinControl; const WithEvents: Boolean = True); var I: Integer; CurrentControl, ClonedControl: TControl; begin for I := ParentSource.ControlCount - 1 downto 0 do begin CurrentControl := ParentSource.Controls[I]; ClonedControl := TControlClass(CurrentControl.ClassType).Create(CurrentControl.Owner); ClonedControl.Parent := ParentSource; CloneProperties(CurrentControl, ClonedControl); ClonedControl.Name := CurrentControl.Name + '_'; if WithEvents then CloneEvents(CurrentControl, ClonedControl); end; end; procedure TForm1.Button1Click(Sender: TObject); begin DuplicateChildren(Panel1); end; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload