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;| Component | LCL class and what is most used |
|---|---|
| Label | TLabel: Caption, Font, Color |
| Button | TButton (or TDmsButton when it has a color or is tall): Caption, Enabled |
| Entry, Password | TEdit: Text, Clear, SetFocus |
| Text | TMemo: Text, Lines.Add, Lines.LoadFromFile |
| Check box, Radio button | TCheckBox, TRadioButton: Checked |
| List, Combo box | TListBox, TComboBox: Items, ItemIndex |
| Scale, Progress bar | TTrackBar, TProgressBar: Position, Max |
| Frame, Page control | TPanel, TPageControl (each page, Pages1_page0…) |
| Status bar | TStatusBar: SimpleText |
| Menu, Timer | TMainMenu, TTimer: Enabled, Interval |
| Open and Save dialogs | TOpenDialog, TSaveDialog: Execute, FileName |
| The window and the studio | Caption, Close, ShowMessage(text), Ask(question), NumToStr(x), ReadText, WriteText, LastKey (in the key event) |
Compile, run and export
- Run (F9) compiles with
lazbuildand opens the application. Errors show in the Output tab with theirForm1.pasline, such asForm1.pas(22,3) Error: Identifier not found. - If Lazarus is missing, the studio says so and offers Install (on macOS, with Homebrew:
brew install fpcandbrew install --cask lazarus). On an Apple Silicon Mac it compiles for arm64. - What
WriteLnwrites shows in the Output tab. To step through the code, openbuild/pascal/app.lpiin the Lazarus IDE. - Export the Lazarus project creates a folder with
app.lpiand the units: open it in Lazarus or build it withlazbuild app.lpi.