Using Multiple Forms

It should take you just a few minutes to use the code on this page. See the following pages for an example of creating multiple forms in code and instructions to create a simple modal form.
  1. Start a new Visual Components Project and save the project as MultipleFormsDemo.
  2. Add a new form named Form2 by clicking on the second icon in the Project manager:
    Adding a Form

    Adding a Form

  3. Add Form3.
  4. Add at the left of each form a TW3Header (from the Mobile tab) and TW3Label (from the Common tab).
  5. Change the captions of the labels (in the Property inspector) to Form 1, Form 2 and Form 3.
  6. Copy and paste the Smart Pascal code of the three forms to replace the default code.
  7. Select Unit1 and save it as uMultipleFormsDemo.
  8. Copy and paste the code of uMultipleFormsDemo to replace the existing code.
  9. Execute!

The Smart Pascal code of uMultipleFormsDemo and the three forms and finally the XML code of Form1 follow the demonstration. (The XML code of the other two forms is the same as that of Form1 except for the name of the form and the caption of the label).

Demonstration

If the demo does not work in your current browser, use another such as Chrome. If you see no display at school, the security system might have blocked it. You can try instead this direct link to the program running on its own page.

MultipleForms.html

Smart Pascal Code of Unit uMultipleFormsDemo

unit uMultipleFormsDemo;

interface

uses
  SmartCL.System, SmartCL.Controls, SmartCL.Components, SmartCL.Forms,
  SmartCL.Application, Form1, Form2, Form3;

type
  TApplication = class(TW3CustomApplication)
  public
    procedure ApplicationStarting; override;
  end;

implementation

procedure TApplication.ApplicationStarting;
var
  mForm: TW3CustomForm;
begin
  mForm := TForm1.Create(display.view);
  mForm.name := 'Form1';
  RegisterFormInstance(mForm, true);
  mForm := TForm2.Create(display.view);
  mForm.name := 'Form2';
  RegisterFormInstance(mForm, false);
  mForm:=TForm3.Create(display.view);
  mForm.name := 'Form3';
  RegisterFormInstance(mForm, false);
  inherited;
end;

end.

Smart Pascal Code of Form1

unit Form1;

interface

uses
  SmartCL.System, SmartCL.Graphics, SmartCL.Controls, SmartCL.Components, SmartCL.Forms,
  SmartCL.Fonts, SmartCL.Borders, SmartCL.Application, SmartCL.Controls.Label;

type
  TForm1 = class(TW3form)
  private
    {$I 'Form1:intf'}
  protected
    procedure InitializeObject; override;
    procedure Resize; override;
end;

implementation

procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}
  W3HeaderControl1.Title.Caption := 'Form1 Header';
  W3HeaderControl1.NextButton.Visible := true;
  W3HeaderControl1.NextButton.OnClick := procedure (Sender: TObject)
    begin
      Application.GotoForm('Form2', feFromRight);
    end;
  W3HeaderControl1.BackButton.Visible := false;
end;
 
procedure TForm1.Resize;
begin
  inherited;
  W3HeaderControl1.Width := ClientWidth;
end;

initialization
  Forms.RegisterForm({$I %FILE%}, TForm1); 
end.

Smart Pascal Code of Form2

unit Form2;

interface

uses 
  System.Types, SmartCL.System, SmartCL.Graphics, SmartCL.Components, SmartCL.Forms,
  SmartCL.Fonts, SmartCL.Borders, SmartCL.Application, SmartCL.Controls.Label,
  SmartCL.Controls.Header;

type
  TForm2 = class(TW3form)
  private
   {$I 'Form2:intf'}
  protected
    procedure InitializeObject; override;
    procedure Resize; override;
  end;

implementation

procedure TForm2.InitializeObject;
begin
  inherited;
   {$I 'Form2:impl'}
   W3HeaderControl1.Title.Caption := 'Form 2 Header';
   W3HeaderControl1.NextButton.Visible := true;
   W3HeaderControl1.NextButton.OnClick := procedure (Sender: TObject)
     begin
       Application.GotoForm('Form3', feFromRight);
     end;
   W3HeaderControl1.BackButton.Visible := true;
   W3HeaderControl1.BackButton.OnClick := procedure (Sender: TObject)
     begin
       Application.GotoForm('Form1', feToLeft);
     end;
end;
 
procedure TForm2.Resize;
begin
  inherited;
  W3HeaderControl1.Width := ClientWidth;
end;
 
initialization
  Forms.RegisterForm({$I %FILE%}, TForm2);
end.

Smart Pascal Code of Form3

unit Form3;

interface

uses 
  System.Types, SmartCL.System, SmartCL.Graphics, SmartCL.Components, SmartCL.Forms,
  SmartCL.Fonts, SmartCL.Borders, SmartCL.Application, SmartCL.Controls.Label,
  SmartCL.Controls.Header;

type
  TForm3 = class(TW3form)
  private
    {$I 'Form3:intf'}
  protected
    procedure InitializeObject; override;
    procedure Resize; override;
  end;

implementation

procedure TForm3.InitializeObject;
begin
  inherited;
  {$I 'Form3:impl'}
  W3HeaderControl1.Title.Caption := 'Form3 Header';
  W3HeaderControl1.NextButton.Visible := false;
  W3HeaderControl1.BackButton.Visible := true;
  W3HeaderControl1.BackButton.OnClick := procedure (Sender: TObject)
    begin
      Application.GotoForm('Form2', feToLeft);
    end;
end;
 
procedure TForm3.Resize;
begin
  inherited;
   W3HeaderControl1.Width := ClientWidth;
end;
 
initialization
  Forms.RegisterForm({$I %FILE%}, TForm3);
end.

XML Code of Form1

<SMART>
  <Form version="2" subversion="1">
    <Created>2014-08-14T12:23:29.094</Created>
    <Modified>2014-08-14T12:23:29.094</Modified>
    <object type="TW3Form">
      <Caption>W3Form</Caption>
      <Zoom>0</Zoom>
      <Width>200</Width>
      <StyleClass>TW3Form</StyleClass>
      <Height>64</Height>
      <BorderRadius>0</BorderRadius>
      <Color>536870911</Color>
      <Name>Form1</Name>
      <object type="TW3HeaderControl">
        <Width>200</Width>
        <StyleClass>TW3HeaderControl</StyleClass>
        <Height>51</Height>
        <BorderRadius>0</BorderRadius>
        <Color>536870911</Color>
        <Name>W3HeaderControl1</Name>
      </object>
      <object type="TW3Label">
        <Caption>Form 1</Caption>
        <Width>128</Width>
        <Top>80</Top>
        <Height>24</Height>
        <Name>W3Label1</Name>
      </object>
    </object>
  </Form>
</SMART>
Programming - a skill for life!

How to use a range of visual components such as edit boxes, combo boxes, grids and charts on one or multiple forms (including a modal form)