This topic describes the following material:
- Event properties and event handlers (事件属性和时间处理方法)
- Triggering multiple event handlers(触发多个事件处理方法)
Contents
[hide]About Events(关于事件)
An event links an occurrence in the system with the code that responds to that occurrence. The occurrence triggers the execution of a procedure called an event handler. The event handler performs the tasks that are required in response to the occurrence. Events allow the behavior of a component to be customized at design-time or at run time. To change the behavior of the component, replace the event handler with a custom event handler that will have the desired behavior.
一个事件链接系统中一个事件发生和对这个事件放生响应代码。当事件发生时被触发执行的过程被称作事件处理方法。事件处理方法执行一个要求响应事件发生的任务,如果要实现期望的行为,那么需要使用自定义的事件处理方法替换原来的事件处理方法。
Event Properties and Event Handlers(事件属性和事件处理方法)
Components that are written in Delphi use properties to indicate the event handler that will be executed when the event occurs. By convention, the name of an event property begins with "On", and the property is implemented with a field rather than read/write methods. The value stored by the property is a method pointer, pointing to the event handler procedure.
在 Delphi的组件中,用属性表示事件处理方法,这些方法在事件发生的时候将会被执行。通常上,事件属性的名字是以On开头的,事件属性是使用字段实现,而不是使用read/write方法的方式。储存在属性中的值是方法的指针,指向事件处理方法的过程。
In the following example, the TObservedObject class includes an OnPing event, of type TPingEvent. The FOnPing field is used to store the event handler. The event handler in this example, TListener.Ping, prints 'TListener has been pinged!'.
program EventDemo; {$APPTYPE CONSOLE} type { Define a procedural type } TPingEvent = procedure of object; { The observed object } TObservedObject = class private FPing: TPingEvent; public property OnPing: TPingEvent read FPing write FPing; { Triggers the event if anything is registered } procedure TriggerEvent(); end; { The listener } TListener = class procedure Ping; end; procedure TObservedObject.TriggerEvent; begin { Call the registerd event only if there is a listener } if Assigned(FPing) then FPing(); end; procedure TListener.Ping; begin Writeln('TListener has been pinged.'); end; var ObservedObject: TObservedObject; Listener: TListener; begin { Create object instances } ObservedObject := TObservedObject.Create(); Listener := TListener.Create(); { Register the event handler } ObservedObject.OnPing := Listener.Ping; { Trigger the event } ObservedObject.TriggerEvent();//Should output 'TListener has been pinged' Readln; // Pause console before closing end.
Triggering Multiple Event Handlers(触发多事件处理方法)
In Delphi, events can be assigned only a single event handler. If multiple event handlers must be executed in response to an event, the event handler assigned to the event must call any other event handlers.
在Delphi中,事件仅能被赋值一个事件处理方法(这个和.net是不同的,.net事件天生带有多播特性(multicast)),如果响应一个事件必须执行多个事件处理方法的话,那么赋值给事件的事件处理方法必须要调用所有其他的事件处理方法。
In the following code, a subclass of TListener called TListenerSubclass has its own event handler called Ping2. In this example, the Ping2 event handler must explicitly call the TListener.Ping event handler in order to trigger it in response to the OnPing event: