[Delphi] how to add events and event processors to Delphi programs

Source: Internet
Author: User
Henan Institute of Financial Management
Pan hanjie

---- Delphi is a powerful visual program development tool. While developing WINDOWS applications using Delphi, Delphi provides many properties and events for each visualization component ), however, in actual applications, you may encounter some special events that you need. These special events are not provided by Delphi. In this case, we need to add these special events to the application. When these events occur, you can immediately call the process of processing these events. This document describes how to add and process events in an application through examples.

---- In Delphi, an event is actually a specialized attribute. It is a procedure pointer. To add an event, you must first specify a pointer to the event process in the defined class. This pointer is used when an event occurs, the pointer is used to process the event. At last, publish the defined event attributes and the associated event processing pointer through the specified 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 points to process toobig1. Put three edit boxes on the form (Form1) of Delphi: Edit1, Edit2, and Edit3, respectively, and put a button Button1. Set the private Integer Variables val1, val2, and res in the program. The variables res are used to record the product of val1 and val2 and are displayed in edit3. When there is a data input through Edit1 and Edit2 greater than 100, an event is triggered and the TooBig1 event processing process is called. A dialog box is displayed, indicating that the event has occurred and has been processed. The source code is as follows. The program passes debugging in Delphi2010 + windows7.

 

 

1 unit unit1;
2 Interface
3 uses
4 windows, messages, sysutils, classes,
5 graphics, controls, forms, dialogs,
6 stdctrls;
7 Type
8 tform1 = Class (tform)
9 edit1: tedit; {enter the first integer}
10 edit2: tedit; {enter the second integer}
11 edit3: tedit; {output the product of the first two integers}
12 button1: tbutton;
13 procedure button1click (Sender: tobject );
14 procedure toobig1 (Sender: tobject );
15 {call this process after an event is triggered}
16 procedure formcreate (Sender: tobject );
17 private
18 val1, val2, Res: integer; {val1 and val2 store Input
Two Integers of 19. Res stores the product of two numbers}
20 ftoobig: tpolicyevent; {defines a point to event
21 processor pointer ftoobig}
22 {private Declarations}
23 public
24 {public declarations}
25 published
26 property ontoobig: tpolicyevent read
27 ftoobig write ftoobig; {define event}
28 end;
29
30 var
31 Form1: TForm1;
32 implementation
33 {$ R *. DFM}
34 procedure TForm1.Button1Click (Sender: TObject );
35 begin
36 val1: = StrToInt (Edit1.Text );
37 val2: = StrToInt (Edit2.Text );
38 if (val1 <100) and (val2 <100) then
39 begin
40 res: = val1 * val2;
41 Edit3.Text: = IntToStr (res );
42 end
43 else
44 if assigned (FTooBig) then OnTooBig (Self );
45 end;
46
47 procedure TForm1.TooBig1 (Sender: TObject );
48 begin
49 Application. MessageBox ('too Big ', 'test Event! ', MB_ OK );
50 end;
51
52 procedure TForm1.FormCreate (Sender: TObject );
53 begin
54 val1: = 1;
55 val2: = 1;
56 FTooBig: = TooBig1; {point the event processing pointer to the event processor}
57 end;
58
59 end.
60
61

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.