Event Descriptors (TNotifyEvent) and Usage Mechanism in Delphi

Alen IBRIC
4 min readMay 23, 2023

I am excited to share with you about Component Event Descriptors, specifically TNotifyEvent, and its usage mechanism in Delphi.

TNotifyEvent is a type of event descriptor that is commonly used in Delphi to handle events. It is a pointer to a procedure that takes two parameters: Sender and EventArgs. Sender is the object that triggered the event, while EventArgs is an optional parameter that provides additional information about the event.

To use TNotifyEvent, you need to declare a variable of type TNotifyEvent and assign it to a procedure that will handle the event. Here’s an example:

type

TForm1 = class(TForm)

Button1: TButton;

procedure Button1Click(Sender: TObject);

private

procedure MyButtonClick(Sender: TObject);

public

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

var

MyEvent: TNotifyEvent;

begin

MyEvent := MyButtonClick;

Button1.OnClick := MyEvent;

end;

procedure TForm1.MyButtonClick(Sender: TObject);

begin

ShowMessage(‘Button clicked!’);

end;

In this example, we have a form with a button named Button1. When the user clicks on the button, the Button1Click procedure is called. Inside this procedure, we declare a variable of type TNotifyEvent named MyEvent and assign it to the MyButtonClick procedure. Finally, we assign MyEvent to the OnClick event of Button1.

When the user clicks on the button, the MyButtonClick procedure is called, which displays a message box with the text “Button clicked!”.

TNotifyEvent can also be used to handle events for custom components. Here’s an example:

type

TMyButton = class(TButton)

private

FOnMyClick: TNotifyEvent;

protected

procedure Click; override;

published

property OnMyClick: TNotifyEvent read FOnMyClick write FOnMyClick;

end;

procedure TMyButton.Click;

begin

inherited;

if Assigned(FOnMyClick) then

FOnMyClick(Self);

end;

In this example, we have a custom component named TMyButton that inherits from TButton. We have also declared a new event named OnMyClick of type TNotifyEvent.

When the user clicks on the button, the Click procedure is called. Inside this procedure, we call the inherited Click procedure to perform the default button behavior. We then check if the OnMyClick event is assigned and call it if it is.

To use this custom component, you can simply assign a procedure to the OnMyClick event. Here’s an example:

procedure TForm1.MyButtonClick(Sender: TObject);

begin

ShowMessage(‘MyButton clicked!’);

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

MyButton1.OnMyClick := MyButtonClick;

end;

In this example, we have a form with a custom component named MyButton1. In the FormCreate event, we assign the MyButtonClick procedure to the OnMyClick event of MyButton1.

Example 1:Using TNotifyEvent with a Timer component

type

TForm1 = class(TForm)

Timer1: TTimer;

procedure Timer1Timer(Sender: TObject);

private

procedure MyTimerEvent(Sender: TObject);

public

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Timer1Timer(Sender: TObject);

var

MyEvent: TNotifyEvent;

begin

MyEvent := MyTimerEvent;

Timer1.OnTimer := MyEvent;

end;

procedure TForm1.MyTimerEvent(Sender: TObject);

begin

ShowMessage(‘Timer event!’);

end;

In this example, we have a form with a Timer component named Timer1. When the Timer1Timer event is triggered, we declare a variable of type TNotifyEvent named MyEvent and assign it to the MyTimerEvent procedure. Finally, we assign MyEvent to the OnTimer event of Timer1.

When the timer interval is reached, the MyTimerEvent procedure is called, which displays a message box with the text “Timer event!”.

Example 2: Using TNotifyEvent with a custom component that has multiple events

type

TMyComponent = class(TComponent)

private

FOnMyEvent1: TNotifyEvent;

FOnMyEvent2: TNotifyEvent;

protected

procedure DoMyEvent1;

procedure DoMyEvent2;

public

procedure TriggerMyEvent1;

procedure TriggerMyEvent2;

published

property OnMyEvent1: TNotifyEvent read FOnMyEvent1 write FOnMyEvent1;

property OnMyEvent2: TNotifyEvent read FOnMyEvent2 write FOnMyEvent2;

end;

procedure TMyComponent.DoMyEvent1;

begin

if Assigned(FOnMyEvent1) then

FOnMyEvent1(Self);

end;

procedure TMyComponent.DoMyEvent2;

begin

if Assigned(FOnMyEvent2) then

FOnMyEvent2(Self);

end;

procedure TMyComponent.TriggerMyEvent1;

begin

DoMyEvent1;

end;

procedure TMyComponent.TriggerMyEvent2;

begin

DoMyEvent2;

end;

In this example, we have a custom component named TMyComponent that has two events named OnMyEvent1 and OnMyEvent2 of type TNotifyEvent.

We have also declared four procedures: DoMyEvent1, DoMyEvent2, TriggerMyEvent1, and TriggerMyEvent2. DoMyEvent1 and DoMyEvent2 check if the corresponding event is assigned and call it if it is. TriggerMyEvent1 and TriggerMyEvent2 are public procedures that can be called from outside the component to trigger the events.

To use this custom component, you can simply assign procedures to the OnMyEvent1 and OnMyEvent2 events. Here’s an example:

procedure TForm1.MyEvent1(Sender: TObject);

begin

ShowMessage(‘MyEvent1!’);

end;

procedure TForm1.MyEvent2(Sender: TObject);

begin

ShowMessage(‘MyEvent2!’);

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

MyComponent1.OnMyEvent1 := MyEvent1;

MyComponent1.OnMyEvent2 := MyEvent2;

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

MyComponent1.TriggerMyEvent1;

end;

procedure TForm1.Button2Click(Sender: TObject);

begin

MyComponent1.TriggerMyEvent2;

end;

In this example, we have a form with a custom component named MyComponent1. In the FormCreate event, we assign the MyEvent1 and MyEvent2 procedures to the OnMyEvent1 and OnMyEvent2 events of MyComponent1.

When the user clicks on Button1, the TriggerMyEvent1 procedure of MyComponent1 is called, which triggers the OnMyEvent1 event and displays a message box with the text “MyEvent1!”. Similarly, when the user clicks on Button2, the TriggerMyEvent2 procedure of MyComponent1 is called, which triggers the OnMyEvent2 event and displays a message box with the text “MyEvent2!”.

In conclusion, TNotifyEvent is a powerful event descriptor that is commonly used in Delphi to handle events. It provides a simple and flexible mechanism for handling events for both built-in and custom components. As a software engineer, it is important to understand how to use TNotifyEvent effectively to create high-quality software that meets the needs of end-users.

--

--