Programming an event in Delphi on the example of the component of type Button
- Related topics
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.
1. Creating a Windows-application
Create Windows-application in Embarcadero RAD Studio 2010 – Delphi 2010. Detailed information about creating Windows-application in Delphi 2010 is described here.
2. Placing components on the 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):
Fig. 1. Main view of application form
3. Interface developing
Change the “Caption” property of main form Form1 and button Button1.
Set corresponding titles (figure 2).
Fig. 2. Interface feveloping
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.
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.
Fig. 4. “Hello!” message after clicking on the button
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.
- Example of event programming of button click in C#
- An example of event programming in C++ Builder
- Creating a simple dialog application Windows of type “Dialog Based Application” – MS Visual Studio – C++