Events
Events are occurrences of an action, typically a system action such as a button control click or a keypress on a keyboard. Components contain special properties called events; component users can plug code into the event (called event handlers) that executes when the event is invoked.
Plugging Code into Events at Design Time
The events page of a TEdit component lists events such as OnChange, OnClick, and OnDblClick. To component writers, events are really pointers to methods. When users of a component assign code to an event, they create an event handler. For example, when you double-click an event in the Object Inspector's events page for a component, Delphi generates a method to which you add your code, such as the following code for the OnClick event of a TButton component:
TForm1 = class(TForm) Button1: Tbutton; procedure Button1Click(Sender: TObject); end; ... procedure TForm1.Button1Click(Sender: TObject); begin { Event code goes here } end;
This code is generated by Delphi.
Plugging Code into Events at Runtime
It becomes clear how events are method pointers when you assign an event handler to an event programmatically. For example, to link your own event handler to an OnClick event of a TButton component, you first declare and define the method you intend to assign to the button's OnClick event. This method might belong to the form that owns the TButton component, as shown here:
TForm1 = class(TForm) Button1: TButton; ... private MyOnClickEvent(Sender: TObject); // Your method declaration end; ... { Your method definition below } procedure TForm1.MyOnClickEvent(Sender: TObject); begin { Your code goes here } end;
The preceding example shows a user-defined method called MyOnClickEvent() that serves as the event handler for Button1.OnClick. The following line shows how you assign this method to the Button1.OnClick event in code, which is usually done in the form's OnCreate event handler:
procedure TForm1.FormCreate(Sender: TObject); begin Button1.OnClick := MyOnClickEvent; end;
This technique can be used to add different event handlers to events, based on various conditions in your code. You can disable an event handler from an event by assigning nil to the event, as shown here:
Button1.OnClick := nil;
Assigning event handlers at runtime is essentially what happens when you create an event handler through Delphi's Object Inspectorexcept that Delphi generates the method declaration. You can't just assign any method to a particular event handler. Because event properties are method pointers, they have specific method signatures, depending on the type of event. For example, an OnMouseDown method is of the type TMouseEvent, a procedure definition shown here:
TMouseEvent = procedure (Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer) of object;
Therefore, the methods that become event handlers for certain events must follow the same signature as the event types. They must contain the same type, number, and order of parameters.
Events are properties. Similar to data properties, events refer to private data fields of a component. This data field is of the procedure type, such as TMouseEvent. Examine this code:
TControl = class(TComponent) private FOnMouseDown: TMouseEvent; protected property OnMouseDown: TMouseEvent read FOnMouseDown write FOnMouseDown; public end;
Remember that properties refer to private data fields of a component. You can see how events, being properties, refer to private method pointer fields of a component.