Delphi - Component Notification
Components that link to other Components

When creating your own components, there are times where you will want to associate several components together in some cooperative task. Typically, this is accomplished by creating a published property (ie, a property that can be modified at design time using the Object Inspector).

A few components that exhibit this behavior are

There are many others.

Note: This is different than components that are composed of several components. In one case, the sub-components are created by the main component and will always be there. In the other (the subject of this page), the designer adds several separate components to a form and then associates them using the Object Inspector.

The Basics | VCL Examples | Exceptions | Basic Architecture | Designer


The Basics

This is pretty important, whenever any control saves a pointer (reference) to another control, that reference must be set to nil if the referenced component is deleted. Delphi handles this by letting the control that saves the pointer (the master) tell the referenced control "that it cares". When the referenced control is deleted, it looks at its notification list and tells all the controls pointing to it that it is being destroyed. It is the responsibility of each master control to set the associated pointer to nil.

FreeNotification (placed in the property setter) notifies the remote control that this control has a pointer (ie, that its Notification method must be called when the remote control is destroyed).

You must also override the TControl.Notification method so that, when the associated control is deleted, your control will be able to set the pointer to nil.

If this is not done, then the Delphi IDE will crash.

This applies only to controls that are associated using properties and does not apply to controls that are created and destroyed by a master control and saved to an internal variable (which is typically done in a creator).


Delphi VCL Examples

These examples are from the Delphi VCL.

Most controls have published properties for PopupMenu and Action. Typically, both of these can be set in the IDE at design time. If any of the associated components are deleted, then the controls that reference them must be told so that the related pointers can be set to nil. Note that both PopupMenus and Actions can be associated with multiple controls .. and that if a PopupMenu (or Action) is deleted, then all the associated controls need to be notified.

When an associated component is deleted, Notification is called and the parent component must set the pointer to nil.

These are some additional examples showing the typical code structure seen in the VCL (but with comments and additional spacing added by me). Notice that inherited is called so that the PopupMenu and Action properties are still handled.

Basically


Exceptions

TSpinButton is interesting because Based on examples (this is not the only one), it is clear that AComponent.FreeNotification is sometimes optional. However, the IDE works fine when it is called.

According to the TComponent.FreeNotification help

However, since it doesn't hurt to set a notification for components in the same form, it isn't worth the time to check. However, that means that Txyz.Notification will be called twice, once by the component being deleted and once by the form (because every component on the form is automatically called).

The code in the next section shows that FreeNotification checks and ignores components with the same parent (which covers components placed on the same form). It also shows that all the components owned by a component will be automatically called by TComponent.Notification.


Basic Architecture

These are some of the gory details. I provide these for those that want a more complete understanding of how this works.


Designer

When creating a form in design mode, there is another layer of complexity - a Designer object keeps track of what is on the form. Any time the form and the Designer get out of sync, Delphi crashes.


Author: Robert Clemenzi
URL: http:// mc-computing.com / Languages / Delphi / ComponentNotification.html