Creating a form-based application using existing files

For form-based applications in our tutorials and student projects we will make available for download a zip file containing .lfm, .pas and .lpr files. This demonstration in form_menu_demo_d7.zip is an example with several simple units and forms.

  1. Unzip the files to what will be the program folder. (In this case form_menu_demo_d7 will be the default).
  2. CTRL+F11 to open project, navigate to the program folder then open the .dpr file (in this case MenuDemo.dpr).
  3. Press F9 to compile and run.
    You can compile and run the program without further changes. You can also view any of the units or forms because the project file contains the names of the source files and the form to which each corresponds. e.g. uBlue in 'uBlue.pas' {frmBlue}

We show below the contents of MenuDemo.dpr, uMenu.pas, uMenu.dfm, uRed.pas and uRed.dfm.

Project file MenuDemo.dpr

program MenuDemo;
uses
  Forms,
  uBlue in 'uBlue.pas' {frmBlue},
  uGreen in 'uGreen.pas' {frmGreen},
  uMenu in 'uMenu.pas' {frmMenu},
  uRed in 'uRed.pas' {frmRed};
begin
  Application.Initialize;
  Application.CreateForm(TfrmMenu, frmMenu);
  Application.CreateForm(TfrmRed, frmRed);
  Application.CreateForm(TfrmGreen, frmGreen);
  Application.CreateForm(TfrmBlue, frmBlue);
  Application.Run;
end.
      
      

uMenu.pas

unit uMenu;

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;

type
  TfrmMenu = class(TForm)
   btnRed: TButton;
   btnGreen: TButton;
   btnBlue: TButton;
   procedure btnBlueClick(Sender: TObject);
   procedure btnGreenClick(Sender: TObject);
   procedure btnRedClick(Sender: TObject);
  end;

var
  frmMenu: TfrmMenu;

implementation
{$R *.dfm}
uses
  uRed, uGreen, uBlue;

procedure TfrmMenu.btnRedClick(Sender: TObject);
begin
  frmRed.show;
end;

procedure TfrmMenu.btnGreenClick(Sender: TObject);
begin
  frmGreen.show
end;

procedure TfrmMenu.btnBlueClick(Sender: TObject);
begin
  frmBlue.show;
end;

end.
      
      

uMenu.dfm

object frmMenu: TfrmMenu
  Left = 670
  Height = 91
  Top = 255
  Width = 320
  Caption = 'Menu Form'
  ClientHeight = 91
  ClientWidth = 320  
  object btnRed: TButton
    Left = 16
    Height = 25
    Top = 24
    Width = 75
    Caption = 'Red Form'
    OnClick = btnRedClick
    TabOrder = 0
  end
  object btnGreen: TButton
    Left = 120
    Height = 25
    Top = 24
    Width = 80
    Caption = 'Green Form'
    OnClick = btnGreenClick
    TabOrder = 1
  end
  object btnBlue: TButton
    Left = 224
    Height = 25
    Top = 24
    Width = 75
    Caption = 'Blue Form'
    OnClick = btnBlueClick
    TabOrder = 2
  end
end

      
      

uRed.pas

unit uRed;

interface

uses
  Classes, SysUtils,  Forms, Controls, Graphics, Dialogs, StdCtrls;

type
  TfrmRed = class(TForm)
    btnMenu: TButton;
    procedure btnMenuClick(Sender: TObject);
  end; 

var
  frmRed: TfrmRed;

implementation

{$R *.dfm}

uses
  uMenu;

procedure TfrmRed.btnMenuClick(Sender: TObject);
begin
  hide;
  frmMenu.show;
end;

end.
      
      

uRed.dfm

object frmRed: TfrmRed
  Left = 795
  Height = 240
  Top = 270
  Width = 320
  Caption = 'Red Form'
  ClientHeight = 240
  ClientWidth = 320
  Color = clRed
  object btnMenu: TButton
    Left = 16
    Height = 25
    Top = 16
    Width = 75
    Caption = 'Menu'
    OnClick = btnMenuClick
    TabOrder = 0
  end
end

      
      

(The files uGreen.pas and uBlue.pas are very similar to uRed.pas and the forms uGreen.dfm and uBlue.dfm are very similar to uRed.dfm).

Features introduced:
  • Existing files unzipped to program folder

  • Application opened using .dpr file

Programming - a skill for life!

How to set up Lazarus to create, edit and run form-based Object-Pascal applications