How to add events and event processors for Delphi programs

zhaozj2021-02-17  52

Delphi is a very functional visualization program development tool. In the process of using Delphi to develop Windows applications, although Delphi provides a lot of properties and events for each visual component, you may encounter some special events you need in practical applications, these Special event Delphi is not available, then we need to add these special events to the app. When these events occur, the process of processing these events immediately immediately immediately. This article uses an example to explain how to add events and process processes for applications.

In Delphi, the event is actually a specialized attribute, which is a process (Procedure) pointer. To add an event, you should first describe a pointer to point to the event procedure in the defined class, which is the process of processing the event by this pointer once the event is performed. Finally, the event attribute that is defined by the specified Published published and the event processing pointer associated with it.

In this example, ftoobig is the event processing process pointer defined, and ONTOOBIG is the event attribute name. Event Process Pointer FTOOBIG The process TOOBIG1 is directed through the initialization of the program. Place three edit boxes on Delphi's form (Form1), which are Edit1, Edit2, and Edit3, and put a button button1. A private integer variable VAL1, VAL2, and RES are served in the program, and the variable RES is used to record the product of VAL1 and VAL2 and display it with EDIT3. When the data entered via EDIT1 and EDIT2 has a greater than 100, an event is triggered, and the event processing process TOOBIG1 is called to display a dialog, indicating that this event has occurred and has been processed. The source program code is as follows, which is debugged in Delphi 3.

Unit unit1;

Interface

Uses

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 first two integers}

Button1: tbutton;

Procedure Button1Click (Sender: TOBJECT);

Procedure TOOBIG1 (Sender: TOBJECT);

{Call this process after the event is triggered}

Procedure formcreate (Sender: TOBJECT);

Private

VAL1, VAL2, RES: Integer; {VAL1 and VAL2 storage input two integers, RES stores two numbers}

FTOOBIG: TNOTIFYEVENT; {Define a pointer pointing to the event processor ftoobig}

{Private Declarations}

public

{Public declarations}

Published

Property ONTOBIG: TNOTIFYEVENT READ

FTOOBIG WRITE FTOOBIG; {Definition Event}

END;

VAR

FORM1: TFORM1;

IMPLEMENTATION

{$ R * .dfm}

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

Begin

VAL1: = start (edit1.text);

VAL2: = start (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);

Begin

Application.MessageBox ('Too Big', 'Test Event!', MB_OK);

END;

Procedure TFORM1.FormCreate (Sender: TOBJECT);

Begin

VAL1: = 1;

VAL2: = 1;

FTOOBIG: = TOOBIG1; {Make event processing pointer points to event processors}

END;

End.

转载请注明原文地址:https://www.9cbs.com/read-30296.html

New Post(0)