Action assignment, commands execution

Now we would like our program to really do something. We put the button (Button form the Standard entry) in the Form and we want the application to close when the button will be pressed in the running program even though we know we can close the application in other ways. We double-click the button (we will suppose the button will be labelled Button1 - Lazarus automatically gives the components names Button1, Button2, etc.) and the Edit window appears (Figure 10). 

Figure 10: Editing Window
Figure 10: Editing Window

We see that the cursor is set between the begin and end keywords - the editing window is ready to write down the program commands (in the right place). It is important to note the word Button1Click that indicates that what we are now programming between the words begin and end will start whenever we click the button Button1 (during program run). First of all, we will learn a simple Close command (Figure 11). 

Figure 11: Simple Command - close
Figure 11: Simple Command - close

When we run the program (press F9 button) and click the Button1 button, our application will close.

We can have several buttons in the program and we can program a different action for each of them. A double-click on the button in the Form will ensure that, in the Source Editor, a part of the program will be prepared to write new commands.

We can assign multiple actions (commands) to each button which will be processed sequentially (Figure 12):

Figure 12: Command Assignment
Figure 12: Command Assignment

The first command changes the button name "Button1" to "BUTTON" and the second command into "button".

Every character in the command is very important, and if we are mistaken in writing, we make a typo, Lazarus will tell us when you will try to run the application (press F9 button) by an error message. 

A command line Button1.Caption := 'Button';

  • Starts with a component name (component Button1).
  • After the dot follows the name of the setting from the Object Inspector (Caption in this example).
  • We have written in the program that we will change the text on the Button1 button.
  • An important feature is the assignment character (a colon and an equals sign). In this example, we can literally say that the Button1 button will be assigned the name BUTTON.
  • After the assignment character (:=) follows a new value by which we want to replace the original setting. In our example, the new value is a character string 'BUTTON' that must be closed in apostrophes.
  • The command like is ended by a semicolon (;), which we willwrite after each command. Otherwise the program will run an error message at the program start.

Now we have shown how and where to write a command, however, that is only a small part of the Editing Window that Lazarus has prepared for us. We saw only the end of the program, but after scrolling the window, we will see (Figure 13):

It may seem to us that the Source Editor contains a lot of text in which we know only one line we have written so far. We are getting acquainted with the whole program, though, now the only important part of the code is between the words implementation and end (shown in the figure).

The label {$R *.dfm} tells us that our program is working with the form. We will not change or delete this entry.

Button actions are in special blocks beginning with a word procedure and ending with a word end. This structure is created automatically by double-clicking on the button and we cannot write it into the text itself (or delete it manually!). We call this structure a procedure or a subprogram.

Figure 13: Source Editor
Figure 13: Source Editor
Vytvorte si webové stránky zdarma! Táto stránka bola vytvorená pomocou služby Webnode. Vytvorte si vlastný web zdarma ešte dnes! Vytvoriť stránky