Writing your First Form-based Application using Lazarus
Before doing this exercise you must first have Lazarus installed on your PC.
The steps to output "Hello World!" on a form are as follows:
-
Start Lazarus by double-clicking on its Launch icon
, or by using the Start button.
After Lazarus has started, you will see the following menu bar:
-
On the menu bar click on Project.
'Project' is circled in purple in the graphic above.
A drop-down menu will appear:
-
On the drop-down menu, click on New Project ....
The Create a new project dialogue appears:
-
Select Application if not already selected then click on the OK button.
You may see the following Project changed dialogue:
-
If do see the above Project changed dialogue, just click on the No button if you have made no unsaved changes to your previous project.
If you do not see the above dialogue you do not need to do anything in this step.
-
Save all files. Shift+Ctrl+S gives the Save project dialogue followed by the save unit dialogue. Navigate to a suitable folder and save the project as HelloWorld to which Lazarus will give the .lpi extension. Save the unit as uHelloWorld, to which Lazarus will be give the .pas extension. (You will be given the option to change to all lower case but you can keep the mixed case name).
-
Open the View forms dialogue (Shift+F12) then select Form1 and click on the OK button.
-
Click on the Abc button then on the form to add a label component to the form.
The label icon is shown here with its tooltip. (Make sure that the Standard tab of components is selected).
-
View the Object Inspector (F11) and type Hello World! as the caption of the label.
-
Press the F9 key to run the program.
The files will be saved automatically, so you do not need to save them first.
After a small delay, you should see a form appear.
(Our executable at 12.4 MB is rather large for this tiny functionality!)
-
Click on the cross at the top right of the form to make the program terminate. Alternatively, as with console programs, you can click on the Lazarus stop icon (a red square to the right of the start icon).
-
Open the View project units dialogue (Ctrl+F12) then select uHelloWorld.pas
The essential code is:
unit uHelloWorld;
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
end.
-
Press Ctrl+F12 again and view HelloWorld.lpr.
The essential code in
HelloWorld.lpr is simply:
program HelloWorld;
uses
Interfaces, Forms, uHelloWorld;
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
-
View Form1 again, right click on it and select
The text view of our form is:
object Form1: TForm1
Left = 670
Height = 240
Top = 255
Width = 320
Caption = 'Form1'
ClientHeight = 240
ClientWidth = 320
LCLVersion = '0.9.30'
object Label1: TLabel
Left = 38
Height = 21
Top = 29
Width = 86
Caption = 'Hello World!'
ParentColor = False
end
end
-
If you can think of anything we could have made more straightforward for you, then please let us know. If it did not work, then please tell us what went wrong.
If you want to, you can quit from Lazarus by selecting menu item
Alternatively, you can move on to the next topic in this guide. The next program, GreetUser has a little more functionality.