Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 5 of 5 total
Thread How to instantiate a control based on its type?
Fri, Mar 26 2021 9:49 AMPermanent Link

Gyro Cosmic

I'm trying to instantiate a form based on the type of the form.
For example:

procedure TMain.Button1Click(Sender: TObject);
var
 tcc: TControlClass; // holds the type of the form
 frm: TForm;
begin
 // in app, this would be a parameter to a CreateFormByType() function
 tcc := TControlClass(TMyForm.ClassType()); // get the form's type
 
 // how do I instantiate tcc?
 frm := TForm(tcc.NewInstance()); // what is EWB's equivalent for newInstance().
end;

Thanks,
--GCR
Fri, Mar 26 2021 12:10 PMPermanent Link

erickengelke

Avatar

Gyro Cosmic wrote:

I'm trying to instantiate a form based on the type of the form.
For example:

procedure TMain.Button1Click(Sender: TObject);
var
 tcc: TControlClass; // holds the type of the form
 frm: TForm;
begin
 // in app, this would be a parameter to a CreateFormByType() function
 tcc := TControlClass(TMyForm.ClassType()); // get the form's type
 
 // how do I instantiate tcc?
 frm := TForm(tcc.NewInstance()); // what is EWB's equivalent for newInstance().
end;

Thanks,
--GCR

Hi, It looks like you are getting confused based on some other language's way of doing things.  What is it you are trying to accomplish in the big picture?   If you want to create two forms of the same type:

var
 form1, form2;

begin

 form1 := TForm1.Create( application );
 form2 := TForm1.Create(applicaiton);

end;



EWB Programming Books and Component Library
http://www.erickengelke.com
Fri, Mar 26 2021 12:15 PMPermanent Link

Walter Matte

Tactical Business Corporation

var
form1of1, form1of2 : TForm1;    

begin

form1of1 := TForm1.Create( application );
form1of2 := TForm1.Create(applicaiton);

end;
Fri, Mar 26 2021 1:56 PMPermanent Link

Gyro Cosmic

>> It looks like you are getting confused based on some other language's way of doing things.
Yup, that language is Delph. 😉

>> What is it you are trying to accomplish in the big picture?
I'm trying to create a function that will allow me to create a form based on its type.

Here's the stripped down Delphi function:

class function TfrmBase.EmbedForm( var iVar; classType: TComponentClass; parentWin: TWinControl): TfrmBase;
var
   instance: TComponent;
begin
   Result := TfrmBase(iVar);
   if TForm(iVar) <> nil then exit;
   Instance := TComponent( classType.newInstance ); // here's how to create based on type in Delphi
   TComponent(iVar) := Instance;
   try
       Instance.create( parentWin );
       TForm(Instance).parent := parentWin;
       [snip]
   except
       TComponent(iVar) := nil;
       Instance.free;
       raise;
   end;
   Result := TfrmBase(iVar);
end;

And here's my in work progress equivalent for EWB.

class function TMain.EmbedForm(frmRef: TRef; classType: TComponentClass; parentCtrl: TControl);
var
   instance: TControlClass; // or TComponent?
begin
   if TForm(frmRef.RefObj) <> nil then exit;
   instance := TControlClass(classType.WHAT?);  // HOW TO CREATE?
   frmRef.RefObj := instance;
   try
       instance.Create( parentCtrl );
       [snip]
   except
       TComponent(frmRef.RefObj) := nil;
       Instance.free;
       raise;
   end;
end;

The goal being to make the form's type a parameter instead of passing it as a concrete type.

i.e. not do:
form1 := TForm1.Create( mainForm );

but instead the equivalent of:
formType := TForm1; // as set elsewhere
and then call
EmbedForm( var form1, formType, mainForm);

This allows me to put my form creation, pinning, and initialization code in the common function, my form types etc. in an array, and pass them into my function.

Thanks,
--GCR
Tue, May 4 2021 5:06 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com


The easiest way to do this is with a function/method like this:

function CreateFormByClass(AFormClass: TFormControlClass): TFormControl;
begin
  Result:=AFormClass.Create(Application);
end;

procedure TForm1.Button12Click(Sender: TObject);
var
  TempForm: TFormControl;
begin
  TempForm:=CreateFormByClass(TForm3);
  TempForm.Show;
end;

You can do the same thing with the TComponentClass class in the WebCore unit, and you can use the ClassByName function to get the class of any component that is in the EWB component library by name:

https://www.elevatesoft.com/manual?action=viewtopic&id=ewb3&topic=ClassByName

Tim Young
Elevate Software
www.elevatesoft.com
Image