如何使用 Delphi 11和TMS web core在Form1面板中显示Form2?

1szpjjfi  于 2023-10-18  发布在  其他
关注(0)|答案(1)|浏览(112)

我的问题是Form 2不会在下面的代码中显示在Form 1面板中:

procedure TForm1.WebButton1Click(Sender: TObject);
var
  LResult: integer;
  newform: TForm2;

begin
  newform := TForm2.Create(Self);
  newform.Caption := 'test form';

  newform.Parent :=  WebPanel1; // this is the problem it doesn't work

  newform.Popup := false;

  // used to manage Back button handling to close subform
  window.location.hash := 'main';

  // load file HTML template + controls
  await(TForm2, newform.Load());

  // init controls values after loading
  newform.WebLabel1.caption := newform.WebLabel1.caption + ' is working!';

  try
    // excute form and wait for close
    LResult := await(TModalResult, newform.Execute);

    WebLabel1.Caption := WebLabel1.Caption + newform.ModalResult.ToString();

    ShowMessage('Form 2 is closed!');
  finally
    newform.Free;
  end;

end;

我试图使用VCL的方式来实现这一点,但目前为止,该形式是不显示在面板内!

newform.Parent :=  WebPanel1; // this is the problem it doesn't work no error messages

另一种方法是:

frm :=  TForm2.CreateNew(WebPanel1.ElementID, nil);

这是完美的工作,但它不会作为一个模态一样,我的代码以上给予回一个结果。
我的目标是在Form 1中的面板中显示Form 2,并等待用户关闭它,以便它可以向Form 1返回结果(一种简单的模态表单方式)。
有什么办法吗?

wtlkbnrh

wtlkbnrh1#

根据要求,我回答我自己的问题与代码注解,感谢您的兴趣。

public
    { Public declarations }
    newform: TWebform; // I moved it here just to reuse it anywhere
  end;    

    procedure TForm1.WebLabel1Click(Sender: TObject);
    
      procedure AfterCreate(AForm: TObject);
        begin
          { Do What you want to that form after creation }
        end;
    
    begin
      // check if it's there then clear it that is useful for multiple time use 
      if Assigned(newform) then
      begin
        newform.Close;
        newform := nil;
      end;

      // Just a double check if its cleared for testing purposes
      if newform <> nil then
         begin
           showMessage('Form Still Exsists!');
           exit;
         end;

      // the creation starts here
      newform := TForm2.CreateNew(WebPanel1.ElementID, @AfterCreate); //<-- this line did the trick!

      newform.Caption := 'test form';
      newform.Popup := false;
    
      // used to manage Back button handling to close subform
      window.location.hash := 'main';
    end;

相关问题