001 – Programming any event in Delphi 2010

Programming any event in Delphi on the example of the component type “Button

In this topic will be considered how to program (application) an event in Delphi system of any version. Events are generated in the programs as a result of some user action (mouse clicking, mouse moving on screen, button pressing etc.) or programs.

To demonstrate, how the event works, we need to develop an application, in which after pressing on button will be shown the “Hello!” message in the main form of application.

Instructions

1. Creating a Windows-application

Create a Windows-application in Embarcadero RAD Studio 2010 – Delphi 2010.

2. Placing components on a form

  Place on the main form of application one component of TLabel type and one component of TButton type (the “Name” property):

Label1 – for the message “Hello!”;

Button1 – implements the processing of the event to display the message “Hello!“.

After that, when we placed components, the form has following view (fig. 1):

01_02_00_001_01_

Fig. 1. Main view of application form

 

3. Interface designing

Change the “Caption” property of main form Form1 and button Button1.

Set corresponding titles (figure 2).

01_02_00_001_02e

Fig. 2. Interface designing

 

4. Programming an event

It remains to program the event of click on the button “Show“.

As a result of clicking on the button, in the operating system occurs the event of pressing the button, which in Delphi is calling “OnClick“. This event corresponds to the code snippet. System allows to reprogram code of this event on our code, at this point you need to write your own code.

  For writing of event reaction code we need to activate button Button1, after that in Object Inspector we need to select the tab “Events“, after that in list of button events need to find the event which is named “OnClick“, and to do double click by mouse into free area of input string (fig. 3). In Figure 3 we see OnClick event in the Object Inspector, that is circled by red color.

01_02_00_001_03e

Fig. 3. The event OnClick of Button1 component

In the Delphi 2010 system automatically will be created a snippet of program code, which will be called every time after that, when user will click on the Button1 button by mouse. The created code has view:

procedure TForm1.Button1Click(Sender: TObject);
begin

end;

Between words “begin” and “end” we are writing next text:

Label1.Caption := 'Hello!';

So, the listing of subprogram of event handler has the following view:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := 'Hello!';
end;

 

5. Run the program

Run the program (F9 button). Do the click on “Show” button. The result of program execution is shown on the figure 4.

01_02_00_001_04e

Fig. 4. “Hello!” message after clicking on the button

 

The results

In modern programming systems, which designs on Windows-based platform (and other platforms),

In modern programming systems, which are designed for Windows platform (and other platforms), interaction between the user and program is carried with the help of events.

In this topic we considered programming of OnClick event on the example of button component. This event widely using into applications and is realized in some others components of Delphi system.