AddHandler Statement

Version 5

After creating an object, the user can add a handler for an object event procedure, so that when the event occurs, the procedure can be evoked.  This connects COM and .NET objects' events to a user-defined event handler.

Syntax

AddHandler ob, sEvent, procEventUser

AddHandler procEvent, procEventUser

Comments

This statement is new in ATEasy 5.0.

There are two different syntax AddHandler statements, one with early bound and the other with late bound.  And there are also two different syntax RemoveHandler statements.  Depending on which AddHandler is used, its corresponding RemoveHandler must be used, see examples below.

The procedure 'procEventUser' is will be called when the event occurs.

There must be a call to RemoveHandler that removes the handler added with AddHandler.

For the example with DotNet class Object, see below as well as an Example ATEasy provides, DotNet.prj.

Example

! create excel app object

ob=CreateObject("Excel.Application")

#ifdef ATEASY5

! example for using AddHandler event name

AddHandler ob, "OnNewWorkbook", OnNewWorkbookLateBind

#endif

.

.

.

#ifdef ATEASY5

! example for using AddHandler event

RemoveHandler ob, "OnNewWorkbook"

#endif

! create excel application

xlapp=CreateObject("Excel.Application")

#ifdef ATEASY5

! example for using AddHandler event

AddHandler xlapp.OnNewWorkbook, OnNewWorkbookEarlyBind

#endif

.

.

.

#ifdef ATEASY5

! example for using AddHandler event

RemoveHandler xlapp.OnNewWorkbook

#endif

The following statement inserts a button control into a APanel control, pnl on the location (left, top)=(0, 10) and width being 10 and height being (panel heigtht - 10) on a form:

 

btn=frm.InsertControl(GetKeyFromCmd(iCmd), "AButton", 0, 10, 10, pnl.Height-10, pnl.Name)

btn.Font.Size=frm.Font.Size

btn.Caption=GetCommandMember(iCmd, ateCmdFormCaption)

btn.Visible=GetCommandStatus(iCmd, ateCmdStatusVisible, ateCmdShowForm)

btn.Enabled=GetCommandStatus(iCmd, ateCmdStatusEnable)

btn.Tag=iCmd

btn.Picture=m_frmMain.imglMiscLarge.Images(GetCommandMember(iCmd, ateCmdId)).ExtracIcon()

AddHandler btn, "OnClick", OnControlButtonClick

The following example shows the procedure AddHandler attached.  The procedure must have the first parameter Val of the control:

 

Procedure OnControlButtonClick(ctl): Void

--------------------------------------------------------------------------------

ctl: Val AControl

iCmd: enumCommandId

{

! retrieve command index

iCmd=ctl.Tag

 

! execute command's procedure

....

}

 

The following statement create an menu separator and an menu item:

 

! create separator

mnu=mnuParent.InsertMenu("sSeparator1", "", amenuTypeSeparator)

 

! create menu item

mnu=mnuParent.InsertMenu(sMenuName, sMenuCaption, amenuTypeItem)

 

! set menu properties...

mnu.Checked=GetCommandStatus(stMenu.enCmd, ateCmdStatusChecked)

mnu.Prompt=GetCommandMember(stMenu.enCmd, ateCmdDescription)

mnu.Picture=m_frmMain.imglSmall.Images(s).ExtracIcon()

 

! add handler for OnClick()

AddHandler mnu.OnClick, OnMenuClick

 

The following example shows the procedure AddHandler attached.  The procedure must have the first parameter Val of the control:

 

Procedure OnMenuClick(mnu): Void
--------------------------------------------------------------------------------

mnu: Val AMenu

stMenu: structMenu

{

! create menu popup

stMenu=mnu.Tag

....

}

The following example is for .Net class object, cls1 is of type DotNetClass1:

 

! fire class1 event

AddHandler cls1.OnEvent, OnEvent

cls1.FireEvent()

if objEventArgs=Nothing

TestStatus=FAIL

elseif objEventArgs.iClassNumber<>1 AND objEventArgs.sEventDescription<>"Class1 event"

TestStatus=FAIL

endif

The following shows the procedure OnEvent():

 

Procedure OnEvent(objSender, objEventArgs): Void

--------------------------------------------------------------------------------

objSender: Val Object

objEventArgs: Val CustomEventArgs

{

Program.objEventArgs=objEventArgs

}

See Also

RemoveHandler, InsertControl(AForm), InsertMenu(AMenu)