Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 4 of 4 total
Thread Passing form to procedure.
Fri, Aug 14 2015 8:51 AMPermanent Link

squiffy

Telemix Ltd.

Avatar

Probably more of a general Pascal question that EWB, but why can't I do this? Or more specifically, how should I pass a form class to a procedure or function to allow me to create a new instance of it? I'm trying to create a single creation routine for a range of forms that are selected from a menu. The actual routine will do more than just create it, but I've trivialised its function for brevity.

If I create this -

procedure test( myvar : TForm);
var tmp : TForm;
begin
   tmp:=myvar.Create(nil);
   tmp:=myvar.Show;
end;

then call it from elsewhere in the same unit like this

test(TmyFormDefinedInAnotherUnit);

I get
"Unable to get property tcontrol.setparent of undefined or null reference" followed by a big line number from, I assume, the compiled JS.

If I swap out Tfrm... (the class name) for just frm..... (the form's name) I get
"unable to get property create1 of undefined or null reference"

The unit is included with "use"
Fri, Aug 14 2015 8:58 AMPermanent Link

squiffy

Telemix Ltd.

Avatar

Forgot to add if I just do this instead of calling the proc then it works :

Var tmp:TForm;
...
tmp:=TfrmIWantToCreate.Create(nil);
tmp.Show;

I just want to be able to move that bit into a proc or function and have the from class be dynamically passed as a parameter.
Fri, Aug 14 2015 12:16 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

<< Probably more of a general Pascal question that EWB, but why can't I do this? Or more specifically, how should I pass a form class to a procedure or function to allow me to create a new instance of it? I'm trying to create a single creation routine for a range of forms that are selected from a menu. The actual routine will do more than just create it, but I've trivialised its function for brevity. >>

What you want is this type from the WebForms unit:

  TFormControlClass = class of TFormControl;

but you can declare your own, if you want to be more specific:

  TFormClass = class of TForm;

and you can use it like this:

procedure test( myvar : TFormClass);
var tmp : TForm;
begin
   tmp:=myvar.Create(nil);
   tmp.Show;
end;

test(TMyFormClass);

Tim Young
Elevate Software
www.elevatesoft.com
Fri, Aug 14 2015 2:08 PMPermanent Link

squiffy

Telemix Ltd.

Avatar

Works perfectly! Thank you.
Image