Home > Articles > Programming > General Programming/Other Languages

Creating Events in Delphi 6

📄 Contents

  1. Where Do Events Come From?
  2. Defining Event Properties
  • Print
  • + Share This
Events are special properties linked to code that get executed whenever a particular action occurs. This article discusses how events are generated and how you can define your own event properties for your custom Delphi components.
This article is excerpted from Delphi 6 Developer's Guide, by Xavier Pacheco and Steve Teixeira (Sams, 2001, ISBN 0-672-32115-7).
From the author of

Where Do Events Come From?

The general definition of an event is basically any type of occurrence that might result from user interaction, the system, or from code logic. The event is linked to some code that responds to that occurrence. The linkage of the event to code that responds to an event is called an event property and is provided in the form of a method pointer. The method to which an event property points is called an event handler.

For example, when the user clicks the mouse button, a WM_MOUSEDOWN message is sent to the Win32 system. Win32 passes that message to the control for which the message was intended. This control can then respond to the message. The control can respond to this event by first checking to see whether there is any code to execute. It does this by checking to see whether the event property points to any code. If so, it executes that code, or rather, the event handler.

The OnClick event is just one of the standard event properties defined by Delphi. OnClick and other event properties each have a corresponding event-dispatching method. This method is typically a protected method of the component to which it belongs. This method performs the logic to determine whether the event property refers to any code provided by the user of the component. For the OnClick property, this would be the Click() method. Both the OnClick property and the Click() method are defined by TControl as follows:

TControl = class(TComponent)
 FOnClick: TNotifyEvent;
 procedure Click; dynamic;
 property OnClick: TNotifyEvent read FOnClick write FOnClick;

Here is the TControl.Click() method:

procedure TControl.Click;
 if Assigned(FOnClick) then FOnClick(Self);

One bit of essential information that you must understand is that event properties are nothing more than method pointers. Notice that the FOnClick property is defined to be a TNotifyEvent. TNotifyEvent is defined as follows:

TNotifyEvent = procedure(Sender: TObject) of object;

This says that TNotifyEvent is a procedure that takes one parameter, Sender, which is of the type TObject. The directive, of object, is what makes this procedure become a method. This means that an additional implicit parameter that you don't see in the parameter list also gets passed to this procedure. This is the Self parameter that refers to the object to which this method belongs. When the Click() method of a component is called, it checks to see if FOnClick actually points to a method, and if so, calls that method.

As a component writer, you write all the code that defines your event, your event property, and your dispatching methods. The component user will provide the event handler when using your component. Your event-dispatching method will check to see whether the user has assigned any code to your event property and then execute it when code exists.

Event handlers are assigned to event properties either at runtime or at design time. In the following section, we show you how to create your own events, event properties, and dispatching methods.

  • + Share This
  • 🔖 Save To Your Account