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.zip is an example with several simple units and forms. In some cases the set-up is more complicated and we will supply also the .lpi project information file. See the following page for details.

  1. Unzip the files to what will be the program folder. (In this case form_menu_demo will be the default).
  2. In Lazarus type Ctrl+O, navigate to the program folder then open the .lpr file (in this case MenuDemo.lpr).
  3. Select the default type of new project – Application.
    You can compile and run the program without further changes, but in order to view units or forms you must add them to the project. Follow these instructions to achieve this conveniently using the Project Inspector.
  4. Select the menu item Project > Project Inspector then click on the + button in the Project Inspector to see the Add to project dialogue box.
  5. Select the Add Files tab then browse to the program folder. Select all the .pas files and open them.
    The files now appear in the Add to project dialogue box.
  6. Select the same files again:


    Selected files to be added to project

  7. Click the Add files to project button.

    Still in the Object Inspector, you can now double click on a filename to view the code of that unit then press F12 to toggle between a unit and its form.

We show below the contents of MenuDemo.lpr, uMenu.pas, uMenu.lfm, uRed.pas and uRed.lfm.

Project file MenuDemo.lpr

program MenuDemo;
uses
  Interfaces, Forms, uMenu, uRed, uGreen, uBlue;
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, FileUtil, 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 *.lfm}
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.lfm

object frmMenu: TfrmMenu
  Left = 670
  Height = 91
  Top = 255
  Width = 320
  Caption = 'Menu Form'
  ClientHeight = 91
  ClientWidth = 320
  LCLVersion = '0.9.30'
  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, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

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

var
  frmRed: TfrmRed;

implementation

{$R *.lfm}
uses
  uMenu;

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

end.   

uRed.lfm

object frmRed: TfrmRed
  Left = 795
  Height = 240
  Top = 270
  Width = 320
  Caption = 'Red Form'
  ClientHeight = 240
  ClientWidth = 320
  Color = clRed
  LCLVersion = '0.9.30'
  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.lfm and uBlue.lfm are very similar to uRed.lfm).

Features introduced:
  • Existing files unzipped to program folder

  • New application created using .lpr file

  • Units and forms added to project

  • Default name of executable changed

Programming - a skill for life!

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