[lazarus] general Question:

Michael.VanCanneyt at Wisa.be Michael.VanCanneyt at Wisa.be
Tue Nov 21 15:56:00 EST 2000




On Tue, 21 Nov 2000, Shane Miller wrote:

> Hey all,
> 
> If I pass a function a String like
> 
> TFORM
> TButton
> TEdit
> 
> how can I create a control based on that?  I don't want to do
> 
> Look at line 471 of customformeditor.pp.  I am trying to create a control without needing to know what the Typename is.

You should not pass a string in the first place, but a variable 
of type TComponentClass.

Every class you wish to create must be registered anyhow.
(RegisterComponents function of classes.pp)

This gives you a variable of type TComponentClass, and which can be used
to create the component:
 
{
Type
  TComponentClass = Type Of TComponent;
}

Function CReateComponent(ComponentToCreate : TComponentClass; 
                         AOwner : TComponent) : TComponent;

begin
  Result:= ComponentToCreate.Create(AOWner);
  // Do some designer stuff as setting position, name etc...
end;

Using the classname is a complete waste of resources and time.

The following functions in classes.pp are meant for this:

procedure RegisterClass(AClass: TPersistentClass);
procedure RegisterClasses(AClasses: array of TPersistentClass);
procedure RegisterClassAlias(AClass: TPersistentClass; const Alias: string);
procedure UnRegisterClass(AClass: TPersistentClass);
procedure UnRegisterClasses(AClasses: array of TPersistentClass);
procedure UnRegisterModuleClasses(Module: HMODULE);
function FindClass(const ClassName: string): TPersistentClass;
function GetClass(const ClassName: string): TPersistentClass;

Hooking in the following procedural variable:
  RegisterComponentsProc: procedure(const Page: string;
    ComponentClasses: array of TComponentClass);

lets you hook in the component registration routine, so storing all
the class pointers that come in there gives you the list of components
(and hence their names through ComponentClasses[i].classname)

If you need more explanations, mail me personally, I'm not really
actively following threads on the list (waaaayyy too much work :/)

Michael.






More information about the Lazarus mailing list