DMarlon Studio

Chapter 9

Pascal with Lazarus

Native desktop applications in Object Pascal, with the LCL components.

Create a Pascal application

When creating the application, choose Pascal (Lazarus). The code goes in the Form1.pas unit: the TForm1 class inherits from TFormDesign and each event is a method declared in the class with override and written in implementation. The studio adds both parts when you create an event. The components are the usual LCL ones.

type
  TForm1 = class(TFormDesign)
  public
    procedure Button1_click; override;
    procedure Scale1_change(Value: Double); override;
  end;

implementation

procedure TForm1.Button1_click;
begin
  Label1.Caption := 'Hello, ' + Entry1.Text;
  Status.SimpleText := 'Greeted';
end;

procedure TForm1.Scale1_change(Value: Double);
begin
  Bar1.Position := Round(Value);
end;
ComponentLCL class and what is most used
LabelTLabel: Caption, Font, Color
ButtonTButton (or TDmsButton when it has a color or is tall): Caption, Enabled
Entry, PasswordTEdit: Text, Clear, SetFocus
TextTMemo: Text, Lines.Add, Lines.LoadFromFile
Check box, Radio buttonTCheckBox, TRadioButton: Checked
List, Combo boxTListBox, TComboBox: Items, ItemIndex
Scale, Progress barTTrackBar, TProgressBar: Position, Max
Frame, Page controlTPanel, TPageControl (each page, Pages1_page0…)
Status barTStatusBar: SimpleText
Menu, TimerTMainMenu, TTimer: Enabled, Interval
Open and Save dialogsTOpenDialog, TSaveDialog: Execute, FileName
The window and the studioCaption, Close, ShowMessage(text), Ask(question), NumToStr(x), ReadText, WriteText, LastKey (in the key event)

Compile, run and export

  • Run (F9) compiles with lazbuild and opens the application. Errors show in the Output tab with their Form1.pas line, such as Form1.pas(22,3) Error: Identifier not found.
  • If Lazarus is missing, the studio says so and offers Install (on macOS, with Homebrew: brew install fpc and brew install --cask lazarus). On an Apple Silicon Mac it compiles for arm64.
  • What WriteLn writes shows in the Output tab. To step through the code, open build/pascal/app.lpi in the Lazarus IDE.
  • Export the Lazarus project creates a folder with app.lpi and the units: open it in Lazarus or build it with lazbuild app.lpi.