DELPHI AREA
MESSAGE BOARD
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Create a component with a given ClassName value at runtime?

 
   Reply to topic    DELPHI AREA Forum Index -> Delphi Programming
View previous topic :: View next topic  
Author Message
hadipardis
Member


Joined: 17 Jul 2004
Posts: 13

PostPosted: 20/07/04 06:18    Post subject: Create a component with a given ClassName value at runtime? Reply with quote

Hi
The problem is:I defined var s:string
Now If I set s:='TButton' then I want to create a TButton instance and
if I set s:='TEdit' then I want to create a new TEdit instance and so on.
Note that s can get many different values dynamically at runtime,so I
dont want to use any IF or CASE statement.Is there any way to overcome
this problem?If your answer is true please give a practical example!

Thanks
Back to top
View user's profile
Kambiz
Administrator


Joined: 07 Mar 2003
Posts: 420
Location: Tehran, Iran

PostPosted: 20/07/04 09:28    Post subject: Reply with quote

Look at the FindClass definition at Delphi's help.
Back to top
View user's profile Send e-mail Visit poster's website
hadipardis
Member


Joined: 17 Jul 2004
Posts: 13

PostPosted: 20/07/04 15:17    Post subject: Reply with quote

Thanks.
I use this code to show a button on the form:

var
DynamicComponent: TComponent;
CompClass: TComponentClass;
begin
RegisterClasses([TButton, TBitmap, TMetafile]);
CompClass := FindClass('TButton') as TComponentClass ;
DynamicComponent := CompClass.Create(Form1);
with dynamiccomponent do
begin
parent:=form1;
name:='ali';
left:=10;
top:=10;
visible:=true;
end;
except
ShowMessageFmt('Cannot find %s in classlist', ['ClassName']);
end;
but this code effects on the owner(Form1) and it is not true

Important:Note that I dont want use this typecasting!!!!!!!!:

with dynamiccomponent as TButton do
begin
parent:=form1;
name:='ali';
left:=10;
top:=10;
visible:=true;
end;
Now ,How I can overcome this problem?????
Back to top
View user's profile
julian
Member


Joined: 15 Nov 2004
Posts: 1

PostPosted: 15/11/04 03:03    Post subject: Reply with quote

"Application.CreateForm" can do

the declaration:

procedure TApplication.CreateForm(InstanceClass: TComponentClass; var Reference);
var
Instance: TComponent;
begin
Instance := TComponent(InstanceClass.NewInstance);
TComponent(Reference) := Instance;
try
Instance.Create(Self);
except
TComponent(Reference) := nil;
raise;
end;
if (FMainForm = nil) and (Instance is TForm) then
begin
TForm(Instance).HandleNeeded;
FMainForm := TForm(Instance);
end;
end;
Back to top
View user's profile
Display posts from previous:   
   Reply to topic    DELPHI AREA Forum Index -> Delphi Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB 2.0.6 © 2001, 2002 phpBB Group