Delphi is a powerful visual program development tool. When we use Delphi to develop WINDOWS applications, although Delphi provides a lot of properties (PRperty) and events (Event) for each visual component, in actual applications, we may encounter some special events that we need. Delphi does not provide special events, so we need to add these special events to the application. When these events occur, the procedures for handling these events can be called immediately. This article uses examples to illustrate how to add events to the application and process the events.
In Delphi, events are actually specialized properties, which are pointers to a procedure. To add an event, you should first specify a pointer to the event process in the defined class. The function of this pointer is to execute the process pointed by this pointer to handle the event once the event occurs. Finally, the defined event attributes and the event processing pointer associated with them are published through the specifier published.
In this example, FtooBig is the defined event processing process pointer, and OnTooBig is the event attribute name. The event processing process pointer FtooBig is made to point to the process TooBig1 through the initialization of the program. Place three edit boxes on the Delphi form (Form1), namely Edit1, Edit2 and Edit3, and place a button Button1. The program sets up private integer variables val1, val2 and res. The variable res is used to record the product of val1 and val2 and display it using Edit3. When one of the data input through Edit1 and Edit2 is greater than 100, an event will be triggered, and the event handling procedure TooBig1 will be called to display a dialog box indicating that this event has occurred and has been processed. The source program code is as follows. The program has been debugged in Delphi 3.
unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Edit1: TEdit; {Enter the first integer} Edit2: TEdit; {Enter the second integer } Edit3: TEdit; {Output the product of the first two integers} Button1: TButton; procedure Button1Click(Sender: TObject); procedure TooBig1(Sender: TObject); {This procedure is called when the event is triggered} procedure FormCreate(Sender: TObject); privateval1,val2,res:integer; {val1 and val2 store the two input integers, res stores the two Product of numbers} FTooBig : TNotifyEvent; {define a pointer to the event handler FTooBig} { Private declarations } public { Public declarations } publishedproperty OnTooBig:TNotifyevent read FTooBig write FTooBig;{define event} end;var Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);begin val1 := StrToInt(Edit1.Text); val2 : = StrToInt(Edit2.Text); if(val1< 100)and(val2< 100) then begin res := val1*val2; Edit3.Text := IntToStr(res); end else if assigned(FTooBig) then OnTooBig(Self);end;procedure TForm1.TooBig1(Sender: TObject );beginapplication.MessageBox('Too Big',' Test Event! ',MB_OK);end;procedure TForm1.FormCreate(Sender: TObject);begin val1:=1; val2:=1; FTooBig := TooBig1;{Make the event handler pointer point to the event handler}end;end.