Events Are Properties

From RAD Studio
Jump to: navigation, search

Go Up to What Are Events?


Components use properties to implement their events. Unlike most other properties, events do not use methods to implement their read and write parts. Instead, event properties use a private class field of the same type as the property.

By convention, the field's name is the name of the property preceded by the letter F. For example, the OnClick method's pointer is stored in a field called FOnClick of type TNotifyEvent, and the declaration of the OnClick event property looks like this:

type
  TControl = class(TComponent)
  private
    FOnClick: TNotifyEvent;    { declare a field to hold the method pointer }
    .
    .
    .
  protected
    property OnClick: TNotifyEvent read FOnClick write FOnClick;
  end;
class PACKAGE TControl : public TComponent
{
private:
    TNotifyEvent FOnClick;
  .
  .
  .
protected:
    __property TNotifyEvent OnClick = {read=FOnClick, write=FOnClick};
  .
  .
  .
};

To learn about TNotifyEvent and other event types, see the next section, Event Types Are Method-pointer Types.

As with any other property, you can set or change the value of an event at run time. The main advantage to having events be properties, however, is that component users can assign handlers to events at design time, using the Object Inspector.

See Also