| View previous topic :: View next topic |
| Author |
Message |
hadipardis Member
Joined: 17 Jul 2004 Posts: 13
|
Posted: 20/07/04 06:18 Post subject: Create a component with a given ClassName value at runtime? |
|
|
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 |
|
 |
Kambiz Administrator

Joined: 07 Mar 2003 Posts: 420 Location: Tehran, Iran
|
Posted: 20/07/04 09:28 Post subject: |
|
|
| Look at the FindClass definition at Delphi's help. |
|
| Back to top |
|
 |
hadipardis Member
Joined: 17 Jul 2004 Posts: 13
|
Posted: 20/07/04 15:17 Post subject: |
|
|
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 |
|
 |
julian Member
Joined: 15 Nov 2004 Posts: 1
|
Posted: 15/11/04 03:03 Post subject: |
|
|
"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 |
|
 |
|