Free Pascal
LAZARUS

Crash Course Lazarus 1 - Setting up a new project

In this tutorial we'll create a simple application. The accent will not be on what the program does, but rather on how to start a project, how to save the files, add components and add a bit of code.


Preparations

  1. If you haven't done so already, create a new folder \FPLaz on your harddisk.
  2. If you haven't done so already, create directory FirstProject "under" \FPLaz.

Setup of the project

  1. Start Lazarus. If a project is already loaded, in the main menu select File / Close All.
  2. In the main menu, select File / New / Project / Application.

    A new graphical Free Pascal application is created. The title bar of the IDE shows the name of the project: Project1. Several windows appear: the "Object Inspector" on the left, the "Source Editor", and a ready-made "Form1" window partly overlying the Source Editor.
    "Unit1" is the name of the unit that goes with Form1. The file Unit1.pas contains the source code for Form1, the main form of your application.

    FirstProject
  3. For the moment, all these files only exist in the RAM of your computer. You have to save them to disk files.

    Open the File menu and click Save All.
    In the dialog Save project1, select directory \FPLaz\FirstProject.
    Enter FirstProject as the file name and click Save.

    Lazarus saves the project settings in FirstProject.lpi (Lazarus Project Information file) and FirstProject.lpr (Lazarus Program file).
  4. In the next dialog, Save unit1, the right directory is already selected. Leave Unit1.pas as the file name and click Save.
    The additional data for Form1, such as the locations and dimensions of the components, are saved automatically in Unit1.lfm.

Adding components

  1. Click on the window that shows your main form. It that's not visible, either select menu View / Toggle Form/Unit View or simply press function key F12.
  2. Let's start by giving a meaningful caption to the form. In the Object Inspector, click next to Caption and enter FirstProject.
  3. Add a TEdit component to the form:
    in the component palette, on the tab Standard, click on the icon TEditof TEdit.
  4. Click somewhere on Form1. An edit-box with the name Edit1 appears.
  5. In the Object Inspector, click next to the property Text and delete the text "Edit1".
  6. Add a second TEdit to the right of Edit1. Also clear the Text of Edit2.
  7. Add a TButton below the edit-boxes. In the Object Inspector, change its caption to Copy.

    By now, your form should look like this:

    FirstProject
  8. Save your files and test the application (press F9).

Adding an event handler

When a button is clicked in runtime, this generates an event. We can respond to this event with some source code in an event handler.

  1. Select Button1 by single clicking it on Form1.
  2. In the Object Inspector, click on the tab Events. Next, click on the "ellipsis" button (...) next to OnClick. Lazarus will add a skeleton for the event handler Button1Click.
  3. Complete the event handler as follows:

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      Edit2.Text := Edit1.Text;
    end;
  4. Save your files and test the application.
    Type something in the first edit-box and click the Copy button.
  5. Stop the application and exit Lazarus.
  6. Have a look in folder FPLaz\FirstProject: there you'll note the file FirstProject.exe, the executable of our application. Double click it to test. Congratulations!