So let's try something.
The first thing to do is create a folder in a known place to contain your files. There will be quite a few of them. I much prefer to do this rather than rely on the default Windows behaviour of using virtual folders from which it is always a pain to find what you are looking for. You will need to access the folder directly, because in there will be the final executable program file.
I created a folder called C:\DEREK_TRIAL\
for this example program.
This is what you will see when you run Lazarus:
Lazarus (and Delphi) scatter themselves across the screen in 5 basic windows. Each one can be moved around and re-sized in the usual way, to suit yourself.
1. is the main command window. Closing this closes Lazarus. Minimizing this minimizes everything.
2. is the window representing the program which you are creating. This is where you drag and drop the buttons and boxes which make up your program. The dots are a grid to which they can be aligned for a neater result. The dots don't show when the program runs. Program windows are called Forms
3. is the editor window. This where you write the code which makes things happen when the buttons and boxes are clicked. Each form has its own corresponding chunk of code, called a Unit
. Each unit appears in a separate tab in the editor window. Small programs often have only one form and one corresponding unit. The editor and form windows can be toggled to and fro in front of each other by pressing F12 on the keyboard -- which is handy if you are working with large forms on a small screen.
4. is called the Object Inspector
window. This is where you make the settings for each Object
in the program by entering them in the right-hand column. For example if you add a button to a form, this is where you set the text which appears on the button. Almost everything in Lazarus is an object, so this Object Inspector window is used a lot. It is typically arranged as a tall column window. Most programmers seem to like it on the left of the screen, but I prefer it on the right.
5. is the message window. This is where Lazarus reports what's happening -- hopefully on a green background to indicate a successful operation. You can close this window when not needed and it will re-appear when there is something to report.
Other smaller dialog windows appear in the usual way as needed.
The Object Inspector is currently showing the settings for the form object. Lazarus makes some default settings when an object is created. Most of them can be left on the defaults, but you will always need to change a few of them. As you can see I have made some initial changes:
Every object in Lazarus must have a name
, so that it can be referred to in the code. For this form you can see that I have given it the name derek_form
. Spaces are not allowed in names. It could be called anything (beans_on_toast ?) but it is handy to use a sensible name and include a reference to the type of object. Otherwise when you come to read the code again in a few years time (or in my case 5 minutes later) it will be meaningless.
Lazarus is case-insensitive, so derek, DEREK, Derek, DERek, dEreK, all mean exactly the same thing and refer to the same object. Lazarus itself uses camel-case, so that objects and functions have names such as ToggleBox or SaveToFile.
You can do the same if you wish for names, but I prefer to use all lower case separated by underscores: toggle_box, save_to_file. This has the useful result when reading code that I can see immediately which names were created by me, and which were created by Lazarus. So this form is called derek_form
Next we need to set the text which appears in the title bar at the top of the form. This text is called an object's Caption
. Generally the text which appears on the screen on a button, next to tickbox, etc., is called its caption. As you can see I have set this to "Trial Program For Derek". As it is typed in the Object Inspector it appears on the form.
I then saved the project so far, such as it is, by clicking File > Save As...
on the top window.
This is a bit tedious, but gives you a chance to set up the file names for this project. You need only do this once. There are several files to be saved in our new folder, each one will appear in turn in the Save dialog:
The project file
has the extension .lpr
and contains the code for the main executable startup program. I called it derek_project.lpr
The same name will be used for several other files, such as the final executable program, derek_project.exe
Then the file containing the unit
must be named and saved. The file has the extension .pas
and contains the code text from the editor window. I called it derek_main_unit.pas
and you can see that Lazarus updated the unit name on the first line in the editor accordingly.
Lazarus will then automatically create the linked file derek_main_unit.lfm
which contains details of the corresponding form window, such as the size and position of each button.
Having done all that the project can be opened and saved in future simply by opening the project file.
So we are now ready next time to make the program actually do something.
message ref: 21532