AClass:=GetClass('TLabel');
L:=TComponentClass(AClass).Create(Self) as TLabel;
This means, that you can store class names in your application or database and create any components using string representations of class names. It's convenient. But there is a problem. VCL requires preregistering all the classes to link class name and class instance using RegisterClass or RegisterClasses procedures. It's not convenient, because you have to add all the used classes manually. Fortunately, there is a trick. You can just enumerate all the existing components on your form (or even in your application with enumerating the Application.Forms) and register all the classes with the following simple code:
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
AClass: TPersistentClass;
L: TLabel;
begin
for i:=0 to Pred(ComponentCount) do
if Components[i] is TPersistent then
begin
AClass:=TPersistentClass(Components[i].ClassType);
if Assigned(AClass) then RegisterClass(AClass);
end;
end;
0 comments:
Post a Comment