RAD Studio |  |

Events Are Properties

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:

Copy Code

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

Copy Code

    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 runtime. 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.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!