I need the ability to create different objects depending on which
TTreeNode is selected. I don't like to hard code the link between the
TTreeNode and the object to be created, but like to use a file or
dbase :
TreeItem1 = TListView
TreeItem2 = TTreeView
TreeItem3 = TButton
How can I create objects without knowing the type until runtime as in:
AObject :=
(TTypeIsReadFromFile).Create(WhatAboutDifferentConstructors) ???
Appreciate your expertise!
Regards
Chris
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
type
TClassType = Class of TControl;
var
T:TClassType;
C:TControl;
begin
if RadioGroup1.ItemIndex=0 then
T:=TButton
else
T:=TEdit;
c:=T.Create(self);
C.Parent:=self;
C.Left:=X;
C.Top:=Y;
end;
type
TControlClass = class of TControl;
var
ClassRegistry : array[byte] of TControlClass;
ClassCount : integer = 0;
// It's not good to declare fix arrays, but you can modify this
procedure RegisterControlClass(which : TControlClass)
begin
ClassRegistry[ClassCount] := which; // maybe you must check if this
class was registered before
inc(ClassCount);
end;
then you can associate each item with a class name. And when you obtain that
name you can search the class and use it to construct the control object.
function SearchControlClass(const name : string) : TControlClass;
var
i : integer;
begin
i := low(ClassRegistry);
while (i < ClassCount) and (ClassRegistry[i].ClassName <> name) do
inc(i);
if i < ClassCount
then Result := ClassRegistry[i]
else Result := nil;
end;
I did'n test this and neither use the proper structures, but you can see the
point.
Best regards
Med