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
finally patform.Free; end;
if res=mrok then showmessage('OK');

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

The tpatform is the following:

unit fpatform;

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

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


{$R *.dfm}

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


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
object but1: TWebButton
Left = 312
Top = 136
Width = 96
Height = 25
Caption = 'but1'
ChildOrder = 1
HeightPercent = 100.000000000000000000
WidthPercent = 100.000000000000000000
OnClick = but1Click
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

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

a sample project that show the error (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.


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