Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2000-11

general Question:

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Shane Miller

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.

Any help is appreciated.

Shane

Reply by email

Unknown author
to Shane Miller
Shane Miller wrote 11 lines
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.

Reply by email