Delphi for .NET FCL Examples Reference
| EventArgs | namespace |
| System |
Declaration
EventArgs = class()
Description
The base class for classes containing event data.
Example
program System_EventArgs; {$APPTYPE CONSOLE} {$AUTOBOX ON} type // FireEventArgs: a custom event inherited from EventArgs. FireEventArgs = class(EventArgs) private FFerocity: integer; FRoom: string; public constructor Create(room: string; ferocity: Integer); override; property Room : string read FRoom; property Ferocity : integer read FFerocity; end; // Event and a Class with a function that creates // the eventargs and initiates the event FireEventHandler = procedure(sender: System.Object; fe: FireEventArgs) of object; FireAlarm = class private FFireEvent : FireEventHandler; public // event "FireEvent" whose type is // our FireEventHandler "delegate". property FireEvent : FireEventHandler add FFireEvent remove FFireEvent; // This will be the starting point // of our event-- it will create FireEventArgs, // and then raise the event, passing FireEventArgs. procedure ActivateFireAlarm(room: string; ferocity: Integer); end; FireHandlerClass = class public constructor Create(_fireAlarm: FireAlarm); override; strict private // This is the function to be executed // when a fire event is raised procedure ExtinguishFire(sender: System.Object; fe: FireEventArgs); end; FireEventTest = class public class procedure Main; end; { FireEventArgs } constructor FireEventArgs.Create(room: string; ferocity: Integer); begin inherited Create; // The fire event will have // two pieces of information // 1) Where the fire is, and // 2) How "ferocious" it is. self.FRoom := room; self.FFerocity := ferocity; end; { FireAlarm } procedure FireAlarm.ActivateFireAlarm (room: string; ferocity: Integer); var fireArgs: FireEventArgs; begin // Raise the event by invoking the delegate. // Pass in the object that initated the // event (self) as well as FireEventArgs. // The call must match the signature // of FireEventHandler. if Assigned(FFireEvent) then begin fireArgs := FireEventArgs.Create(room, ferocity); FFireEvent(self, fireArgs); end; end; { FireHandlerClass } constructor FireHandlerClass.Create(_fireAlarm: FireAlarm); begin inherited Create; // Add a delegate containing the ExtinguishFire // function to the class' // event so that when FireAlarm is raised, // it will subsequently execute ExtinguishFire. Include(_fireAlarm.FireEvent, ExtinguishFire); end; procedure FireHandlerClass.ExtinguishFire (sender: TObject; fe: FireEventArgs); begin Console.WriteLine(''#10'The ExtinguishFire function was called by {0}.', sender.ToString); if (fe.ferocity < 2) then Console.WriteLine('This fire in the {0} is no problem. I''m going to pour some water on it.', fe.room) else if (fe.ferocity < 5) then Console.WriteLine('I''m using FireExtinguisher to put out the fire in the {0}.', fe.room) else Console.WriteLine('The fire in the {0} is out of control. I''m calling the fire department!', fe.room); end; { FireEventTest } class procedure FireEventTest.Main; var myFireHandler: FireHandlerClass; myFireAlarm: FireAlarm; begin // Create an instance of the class // that will be firing an event myFireAlarm := FireAlarm.Create; // Create an instance of the class that will be // handling the event. Note that // it receives the class that will fire // the event as a parameter. myFireHandler := FireHandlerClass.Create(myFireAlarm); //use our class to raise a few events // and watch them get handled myFireAlarm.ActivateFireAlarm('Kitchen', 3); myFireAlarm.ActivateFireAlarm('Study', 1); myFireAlarm.ActivateFireAlarm('Porch', 5); end; //test program begin FireEventTest.Main; end. |
Example output
The ExtinguishFire function was called by System_EventArgs.FireAlarm. I'm using FireExtinguisher to put out the fire in the Kitchen. The ExtinguishFire function was called by System_EventArgs.FireAlarm. This fire in the Study is no problem. I'm going to pour some water on it. The ExtinguishFire function was called by System_EventArgs.FireAlarm. The fire in the Porch is out of control. I'm calling the fire department!
Looking for Delphi for .NET demos / source code samples? Look no further, Delphi for .NET FCL Examples Reference is what you are searching for!

