Board index » delphi » Creating component by class name?

Creating component by class name?

Hi,

If I have the name of a class of a component as a string, how can I get the
class to create it? Is it possible? For example I have a frame with class
name 'myframe'. I want to create the frame, but I know only the name of the
class as string ('myframe').

I've tryed to get the class type using GetTypeData (passing tkClass as kind
and the name of the class as name), but does not works (access violation at
Create):

var
  Form : TForm;
  Frame : TFrame;
  Inf : TTypeInfo;
  Dat : PTypeData;
...
Inf.Kind := tkClass;
Inf.Name := 'myframe';
Dat := GetTypeData(@Inf);
Frame := TFrame(Dat^.ClassType).Create(Form); // ***** Access violation
*****

Thanks,
Laszlo

 

Re:Creating component by class name?


Yes. Take a look at TypInfo.Pas unit. It has some functions like GetClass
and FindClass which allow you to create an instance of a class given the
class name. Look for 'GetClass' in help for a example
For Ex :-
TempComponent := TComponentClass(GetClass('TEdit')).Create(Nil);
where TempComponent is TComponent. You can then type cast TempComponent to
TEdit type and assign the properties. You will also have to do a
RegisterClass before you call GetClass.

HTH,
Vikram

Quote
"Kertsz Lszl" <kertesz.las...@chello.hu> wrote in message

news:3d2c9bdf$1_1@dnews...
Quote
> Hi,

> If I have the name of a class of a component as a string, how can I get
the
> class to create it? Is it possible? For example I have a frame with class
> name 'myframe'. I want to create the frame, but I know only the name of
the
> class as string ('myframe').

> I've tryed to get the class type using GetTypeData (passing tkClass as
kind
> and the name of the class as name), but does not works (access violation
at
> Create):

> var
>   Form : TForm;
>   Frame : TFrame;
>   Inf : TTypeInfo;
>   Dat : PTypeData;
> ...
> Inf.Kind := tkClass;
> Inf.Name := 'myframe';
> Dat := GetTypeData(@Inf);
> Frame := TFrame(Dat^.ClassType).Create(Form); // ***** Access violation
> *****

> Thanks,
> Laszlo

Re:Creating component by class name?


Yes, I know this, but what to do, when the class is not registered yet?
Before we call GetClass, the class must be registered. So, I must register
first, then create - but how to register when I just the name (as string)
know? Have you any idea?

Thanks,
Laszlo

Quote
> Yes. Take a look at TypInfo.Pas unit. It has some functions like GetClass
> and FindClass which allow you to create an instance of a class given the
> class name. Look for 'GetClass' in help for a example
> For Ex :-
> TempComponent := TComponentClass(GetClass('TEdit')).Create(Nil);
> where TempComponent is TComponent. You can then type cast TempComponent to
> TEdit type and assign the properties. You will also have to do a
> RegisterClass before you call GetClass.

Re:Creating component by class name?


Quote
> Yes, I know this, but what to do, when the class is not registered yet?

Use initialization section. Place there Registering of all Your classes and
of 2rd party classes You need.

PS: i widh TClass had a constructor that i could ooverride, so all my
classes was registered as soon as i decalre them :)

Re:Creating component by class name?


Ok, thanks. This is, what I want to sidestep...

Laszo

Quote
> Use initialization section. Place there Registering of all Your classes
and
> of 2rd party classes You need.

> PS: i widh TClass had a constructor that i could ooverride, so all my
> classes was registered as soon as i decalre them :)

Re:Creating component by class name?


Quote
> > Use initialization section.
>  This is, what I want to sidestep...
> > PS: i wish TClass had a constructor that i could override, so all my
> > classes was registered as soon as i declare them :)

You see, i want it too. But no luck still :)

Re:Creating component by class name?


AFAIK, you have to manually register all possible classes that you wish to
create in this method. Use the Initialization section as suggested.

Vikram

Quote
"Kertsz Lszl" <kertesz.las...@chello.hu> wrote in message

news:3d2d2572_2@dnews...
Quote
> Yes, I know this, but what to do, when the class is not registered yet?
> Before we call GetClass, the class must be registered. So, I must register
> first, then create - but how to register when I just the name (as string)
> know? Have you any idea?

> Thanks,
> Laszlo

> > Yes. Take a look at TypInfo.Pas unit. It has some functions like
GetClass
> > and FindClass which allow you to create an instance of a class given the
> > class name. Look for 'GetClass' in help for a example
> > For Ex :-
> > TempComponent := TComponentClass(GetClass('TEdit')).Create(Nil);
> > where TempComponent is TComponent. You can then type cast TempComponent
to
> > TEdit type and assign the properties. You will also have to do a
> > RegisterClass before you call GetClass.

Other Threads