Tcustomform instead of the inserted form in CreateNewForm

Why am I getting this error?

Also, The following code, does NOT fit the form inside the webpanel and gives strange errors:

procedure Tmain.ButNewPat;               
var patform: Tpatform; res: Integer;
    procedure aftercr(Aform:tobject);
    begin if assigned(aform) then Tpatform(aform).but2.Enabled:=true; end;
begin patform:=Tpatform.CreateNew(webpanel1.ElementID,@aftercr); try
res:=await(integer,patform.execute);
finally patform.Free; end;
if res=mrok then showmessage('OK');
end;

it stops in tcomponent.validaterename and gives the following error:

The tpatform is the following:

unit fpatform;

interface
uses System.SysUtils, System.Classes, JS, Web, WEBLib.Graphics, WEBLib.Controls,
     WEBLib.Forms, WEBLib.Dialogs, Vcl.StdCtrls, WEBLib.StdCtrls, Vcl.Controls;

type
  Tpatform = class(TWebForm)
    patweb1: TWebEdit;
    but1: TWebButton;
    but2: TWebButton;
    procedure but1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


implementation

{$R *.dfm}

procedure Tpatform.but1Click(Sender: TObject);
begin
modalresult:=mrok; close;
end;

end.

object patform: Tpatform
Width = 462
Height = 480
ElementPosition = epRelative
object patweb1: TWebEdit
Left = 128
Top = 136
Width = 121
Height = 22
HeightPercent = 100.000000000000000000
Text = 'patweb1'
WidthPercent = 100.000000000000000000
end
object but1: TWebButton
Left = 312
Top = 136
Width = 96
Height = 25
Caption = 'but1'
ChildOrder = 1
HeightPercent = 100.000000000000000000
WidthPercent = 100.000000000000000000
OnClick = but1Click
end
object but2: TWebButton
Left = 312
Top = 192
Width = 96
Height = 25
Caption = 'but2'
ChildOrder = 2
Enabled = False
HeightPercent = 100.000000000000000000
ModalResult = 2
WidthPercent = 100.000000000000000000
end
end

<html>
  <head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title>TMS Web Project</title>
    <style>
    </style>
  </head>
  <body>
  </body>
</html>

a sample project that show the error
webcorebug.zip (949.2 KB)

The return type of CreateNewForm() is TCustomForm. You try to assign this to a class of the type TpatForm, so cast the result to TpatForm

This line of code:

patform := Tpatform.CreateNew(webpanel1.ElementID,@aftercr);

already creates the hosted form and shows it in the panel. (as also demonstrated in the demo Demo\Basics\FormHosting

There is no need to "execute" this form.

Interesting, thank you
but, How I get the Tmodalresult back?
and what about freeing the form object?

Showing a form hosted in a panel is different from showing a form modal, hence, there is no concept of modalresult in this case.

Demo under Demo\Basics\FormHosting shows how you can remove the form again.

1 Like

So, I believe I should assign an Event procedure from the main form to the button in the embedded form to get the result in the main form and deal with the form object.
For destroying the object the sample application helped me, I should have a shared Twebform object in the main form.

Correct

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.