Hello,
I'm using this code to copy the properties of a control:
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;And, while it works 99% of the time, sometimes it raises a "duplicate name" exception.
I stepped into the code and the exception is generated here
Procedure TComponent.ValidateRename(AComponent: TComponent;
const CurName, NewName: string);
begin
//!! This contradicts the Delphi manual.
If (AComponentNil) and (CompareText(CurName,NewName)0) and
(AComponent.Owner = Self) and
(FindComponent(NewName)Nil) then
raise EComponentError.Createfmt(SDuplicateName,[newname]);
If (csDesigning in FComponentState) and (FOwnerNil) then
FOwner.ValidateRename(AComponent,Curname,Newname);
end;but I cannot see why since an unique name has been generated in TReader.ReadRootComponent
{ We need an unique name }
i := 0;
{ Don't use Result.Name directly, as this would influence
FindGlobalComponent in successive loop runs }
ResultName := CompName;
Lock;
try
while Assigned(FindGlobalComponent(ResultName)) do
begin
Inc(i);
ResultName := CompName + '_' + IntToStr(i);
end;
Result.Name := ResultName;
finally
Unlock;
end;This is with lazarus 3.8 under win32.
Bye
