5-Minute-Snack: Handling multiple forms with TApplication in TMS Web Core Apps
The class TApplication has been part of Delphi since its inception. TMS Web Core tries to make as little changes to your development as possible. Thus, it provides its own method helper class to TApplication to provide additional functionality that will help you to control and react to events in your web application.
TApplication = class(TControl) private public // function CreateNewForm(AInstanceClass: TFormClass): TCustomForm; overload; function CreateNewForm(AInstanceClass: TFormClass; AElementID: string): TCustomForm; overload; procedure CreateForm(AInstanceClass: TFormClass; var AReference); overload; procedure CreateForm(AInstanceClass: TFormClass; AElementID: string; var AReference); overload; procedure CreateForm(AInstanceClass: TFormClass; AElementID: string; var AReference; AProc: TFormCreatedProc); overload; procedure CreateForm(AInstanceClass: TDataModuleClass; var AReference); overload; procedure CreateForm(AInstanceClass: TFormClass; AElement: TJSHTMLElement; var AReference); overload; property ActiveForm: TCustomForm read FActiveForm; property MainForm: TCustomForm read FMainForm; end;
You have plenty of methods to create a new form. These methods have been extended to fulfill the needs of the asynchronous nature of the web. To be precise, every method overs a callback that can be used to implement code after the form has been created. Of course, every form has its own OnCreate event, but you might want to execute code that is not part of the form after creation. E.g. passing values to the new form.
procedure TForm1.ShowSettings; var LFrm : TFrmSettings; // called after form is closed procedure AfterShowModal(AValue: TModalResult); begin // evaluate AValue and get values from form ... end; // will be called after form has been created procedure AfterCreate(AForm: TObject); begin // pass values to form referenced in AForm ... end; begin LFrm := TFrmSettings.CreateNew( @AfterCreate ); LFrm.ShowModal( @AfterShowModal ) end;
The example above is taken from a TMS Web Core application that features a form with settings. The form is being created and shown. Its values for initialization are passed in AfterCreate. Changes can be persisted in AfterShowModal.
Talking about multiple forms in an app. You can always query the active form that is being displayed using:
LActiveForm := Application.ActiveForm;
Further, to determine the main form of the application use:
LMainForm := Application.MainForm;