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.
- If you haven't done so already, create a new folder \FPLaz on your harddisk.
- If you haven't done so already, create directory FirstProject "under" \FPLaz.
Setup of the project
- Start Lazarus. If a project is already loaded, in the main menu select File / Close All.
- 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.
- For the moment, all these files only exist in the RAM of your computer. You have to save them to
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).
- 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.
- 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.
- Let's start by giving a meaningful caption to the form. In the Object Inspector, click next to Caption and enter FirstProject.
- Add a TEdit component to the form:
in the component palette, on the tab Standard, click on the icon of TEdit.
- Click somewhere on Form1. An edit-box with the name Edit1 appears.
- In the Object Inspector, click next to the property Text and delete the text "Edit1".
- Add a second TEdit to the right of Edit1. Also clear the Text of Edit2.
- Add a TButton below the edit-boxes. In the Object Inspector, change its caption to Copy.
By now, your form should look like this:
- 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.
- Select Button1 by single clicking it on Form1.
- 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.
- Complete the event handler as follows:
procedure TForm1.Button1Click(Sender: TObject);
Edit2.Text := Edit1.Text;
- Save your files and test the application.
Type something in the first edit-box and click the Copy button.
- Stop the application and exit Lazarus.
- 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!