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:

  1. Start Lazarus by double-clicking on its Launch icon You should be able to find a launch icon like this on your desktop, or by using the Start button.

    After Lazarus has started, you will see the following menu bar:


    The Lazarus Menu Bar, with 'Project' circled in purple to help you find it.

  2. On the menu bar click on Project.
    'Project' is circled in purple in the graphic above.

    A drop-down menu will appear:


    The Project menu

  3. On the drop-down menu, click on New Project ....

    The Create a new project dialogue appears:


    The Lazarus 'Create a new project' dialogue, with 'Application' selected

  4. Select Application if not already selected then click on the OK button.

    You may see the following Project changed dialogue:


    Lazarus 'Project changed' dialogue, which may or may not appear, depending on circumstances

  5. 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.
  6. 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).
  7. Open the View forms dialogue (Shift+F12) then select Form1 and click on the OK button.
  8. Click on the Abc button then on the form to add a label component to the form.

    Label on form

    The label icon is shown here with its tooltip. (Make sure that the Standard tab of components is selected).

  9. View the Object Inspector (F11) and type Hello World! as the caption of the label.

    Caption of label

  10. 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.


    Output

    (Our executable at 12.4 MB is rather large for this tiny functionality!)

  11. 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).
  12. 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.        
    
  13. 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.         
    
  14. View Form1 again, right click on it and select View Source (.lfm)
    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  
    
  15. 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.
Features introduced:
  • New form-based application opened

  • Project and unit named

  • Label component used

  • Text output to screen using label

If you want to, you can quit from Lazarus by selecting menu item File > Quit

Alternatively, you can move on to the next topic in this guide. The next program, GreetUser has a little more functionality.

Programming - a skill for life!

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