delphitframetms-web-core

How do I create a TWebFrame dynamically from code and show it?


So basically I have a TMS Web Core App and I've created a TWebFrame with some random components on it. I want to dynamically create this frame and show it on my main form, but whenever I try to do this, I just see a blank frame without any of the components on it.

Here's my frame:

Delphi TWebFrame with random components

unit Unit2;

interface

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

type
  TFrameDemo = class(TWebFrame)
    WebLabel1: TWebLabel;
    WebPanel1: TWebPanel;
    WebLabel2: TWebLabel;
    WebMemo1: TWebMemo;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrameDemo: TFrameDemo;

implementation

{$R *.dfm}

end.

The way I am trying to create it is simply by creating it as you would in VCL or FMX. Here's a simple example where I'm doing it from the OnCreate event on the main form:

procedure TFormMain.WebFormCreate(Sender: TObject);
var
  aFrame: TWebFrame;
begin
  aFrame := TFrameDemo.Create(Application);
  aFrame.Name := 'MyFrame';
  aFrame.Visible := True;
  aFrame.Parent := FormMain;
  aFrame.Align := alClient;
end;

But this doesn't seem to work. The frame is created, but it's empty. You can see that by inspecting the DOM via the browser:

TMS Web Core Delphi project in the browser

Why is the frame empty? Why is it not creating the components on the frame? What is the correct way to dynamically create a TWebFrame in TMS Web Core with Delphi?


Solution

  • Turns out you need to call the LoadFromForm function on the created frame:

    procedure TFormMain.WebFormCreate(Sender: TObject);
    var
      aFrame: TWebFrame;
    begin
      aFrame := TFrameDemo.Create(Application);
      aFrame.Name := 'MyFrame';
      aFrame.Visible := True;
      aFrame.Parent := FormMain;
      aFrame.Align := alClient;
    
      aFrame.LoadFromForm; // This is needed otherwise it won't create and show components on the frame
    end.
    

    If you don't call aFrame.LoadFromForm after the frame has been created, then it won't create any of the sub-components on the frame.