Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This more generic routine works either with a Form or Frame (updated to use a subclass for the new control):</p> <pre><code>function ReplaceControlEx(AControl: TControl; const AControlClass: TControlClass; const ANewName: string; const IsFreed : Boolean = True): TControl; begin if AControl = nil then begin Result := nil; Exit; end; Result := AControlClass.Create(AControl.Owner); CloneProperties(AControl, Result);// copy all properties to new control // Result.Left := AControl.Left; // or copy some properties manually... // Result.Top := AControl.Top; Result.Name := ANewName; Result.Parent := AControl.Parent; // needed for the InsertControl &amp; RemoveControl magic if IsFreed then FreeAndNil(AControl); end; function ReplaceControl(AControl: TControl; const ANewName: string; const IsFreed : Boolean = True): TControl; begin if AControl = nil then Result := nil else Result := ReplaceControlEx(AControl, TControlClass(AControl.ClassType), ANewName, IsFreed); end; </code></pre> <p>using this routine to pass the properties to the new control </p> <pre><code>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; </code></pre> <p>use it like:</p> <pre><code>procedure TFrame1.AfterConstruction; var I: Integer; NewEdit: TMyEdit; begin inherited; NewEdit := ReplaceControlEx(Edit1, TMyEdit, 'Edit2') as TMyEdit; if Assigned(NewEdit) then begin NewEdit.Text := 'My Brand New Edit'; NewEdit.Author := 'Myself'; end; for I:=0 to ControlCount-1 do begin ShowMessage(Controls[I].Name); end; end; </code></pre> <p><strong>CAUTION</strong>: If you are doing this inside the AfterConstruction of the Frame, beware that the hosting Form construction is not finished yet.<br> Freeing Controls there, might cause a lot of problems as you're messing up with Form controls housekeeping.<br> See what you get if you try to read the new Edit Caption to display in the ShowMessage...<br> In that case you would want to use<br> ...ReplaceControl(Edit1, 'Edit2', <strong>False</strong>)<br> and then do a<br> ...FreeAndNil(Edit1)<br> later.</p>
    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. 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.
 

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