Free Form

<< Click to Display Table of Contents >>

Navigation:  Technology Overview > Forms and Modules > Other Forms >

Free Form

This is the auto-generated code for a free form (already renamed):

 

unit _FreeForm;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, uniGUITypes, uniGUIAbstractClasses,
  uniGUIClasses, uniGUIForm;
 
type
  TFreeForm = class(TUniForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
implementation
 
uses
  uniGUIApplication;
 
{$R *.dfm}
 
end.

 

The main difference between this free form and an application form is the absence of the function, the equivalent of the VCL global variable.

 

But the important difference is that, by requesting a free form, the user is planning to do things which are not possible with an application form:

 

It will be possible to create several instances of the form (for example, leaving several non-modal forms on the screen showing different records of a dataset)

The user will have tighter control over the form lifetime.

 

The next example shows the same form, but modified to capture a text as a modal form:

 

unit _FreeForm;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, uniGUITypes, uniGUIAbstractClasses,
  uniGUIClasses, uniGUIForm, uniButton, uniGUIBaseClasses, uniEdit;
 
type
  TFreeForm = class(TUniForm)
    edtText: TUniEdit;
    btnOk: TUniButton;
    btnCancel: TUniButton;
  private
    function GetText: string;
  public
    property Text : string read GetText;
  end;
 
  function GetText : string;
 
implementation
 
uses
  uniGUIApplication;
 
{$R *.dfm}
 
function GetText : string;
var
  frm : TFreeForm;
begin
  frm := TFreeForm.Create(uniGUIApplication.UniApplication);
 
  if frm.ShowModal = mrOk then
    Result := frm.Text
  else
    Result := ''
end;
 
{ TFreeForm }
 
function TFreeForm.GetText: string;
begin
  Result := edtText.Text;
end;
 
end.

 

The owner of the free form is not the global application, but the instance handling the current session.

Notice also that the form (frm) is automatically released (FreeOnClose is true and a value was assigned to ModalResult).

The variable itself will be released after going out-of-scope (when exiting the function GetText).

 

Free forms are convenient when the user is interested in services like GetText instead of on the form. If that is the case, the form will always be created and released as part of the service implementation and shouldn't be exposed to the rest of the program. What the program is expecting is just the service.