Skip to content

Lazarus! Get up, pick your pc up, and program!

Object Inspector - Click Event

Those and more info are available in the Object Inspector.
Here you have the hierarchical tree of objects on top (you see that Button belongs to Form: in jargon Form is father and Button is child); down we have direct properties, such as width and height, or left (left distance from direct father object) and top, and so on.
Typically a button is intended to be pressed: nothing special, you'd say.
Point here is to make compiler able to bind button's click to "press" action and its consequences.
In technical words we must create an event  (clicking, for example) and associate it to (a series of) actions.

From here go to Events tab, on OnClick row, click the suspension points. What happens?

Button Click Event - Code

The answer in picture beside.

Lazarus has added a little portion of code, relative to click event; it is empty, begin and end are like parentheses delimiting the so called block code.
This means Lazarus doesn't know the goal, only the event (which it binds internally) because knows button object with its properties.

We're going to use the event to change one button's property.


Code Completion
Button Object Property

First of all notice a comfortable feature: the code completion.
When you start writing a word, you can press Ctrl+SpaceBar obtaining a suggestion window (this works if Lazarus has solutions to propose).

In this case we need Button1: putting a mark after, completion window appears automatically (after a bit while) offering some possibilities, according with button object. Select Caption.

Be aware that Caption is the visible object's name, but not the real one.
When you put a Button onto a Form, Lazarus usually creates it with Caption and Name properties values equal; they are changeable but with this difference: Name must be unique, while Caption can be shared between more objects.

Tip: What if I have two Buttons with same Name and Caption, one on a Form and one on the other?
Certainly Forms have different Names; this guarantees two Buttons are different, belonging to different fathers!

You're ready to compile with compiler (or to use cooking machine).
Without saving the code into a .pas file, we simply press the green arrow on the toolbar: this way Lazarus (better, FreePascal) will compile on a temporary directory, producing an executable and launching it.

Result is a classical window with a button on, whose caption is Button1: by clicking it we change this to Pressed, while its name Button1 doesn't.