8 11 2 EVTROUTINE Examples

LANSA Technical

8.11.2 EVTROUTINE Examples

The following code displays a message saying "hello" when the user clicks on #HelloBtn or selects the menu item #HelloMit :

EVTROUTINE HANDLING(#HelloBtn.Click #HelloMit.Click)
         MESSAGE MSGTXT('hello')
ENDROUTINE
 

The following code first defines a generic name #AnyComponent for the three push-buttons #PHBN_1, #PHBN_2, #PHBN_3 which Click event is being processed. Then it sets the value of the #Button_Caption field to the caption of the button which is clicked.

EVTROUTINE HANDLING(#PHBN_1.Click #PHBN_2.Click #PHBN_3.Click) COM_SENDER(#AnyComponent)
    #Button_Caption := #AnyComponent.Caption
ENDROUTINE  
 

The following code defines a pop-up menu and a collection for its menu items. When the form is initialized five menu items are created in the collection. In the event routine of the Click event of the menu item collection the items are given a generic name #AnyOfTheGroup. This name is then used to assign the caption of the clicked menu item to a field named #M_Caption.

DEFINE_COM CLASS(#PRIM_PMNU) NAME(#PMNU_1)
DEFINE_COM CLASS(#PRIM_KCOL) NAME(#ITEMS) COLLECTS(#PRIM_MITM) KEYEDBY(#STD_NUM)
 
EVTROUTINE HANDLING(#COM_OWNER.Initialize) OPTIONS(*NOCLEARMESSAGES *NOCLEARERRORS)
   Set Com(#Items<1>) Caption('Item One') Parent(#PMNU_1) 
   Set Com(#Items<2>) Caption('Item Two') Parent(#PMNU_1) 
   Set Com(#Items<3>) Caption('Item Three') Parent(#PMNU_1)
   Set Com(#Items<4>) Caption('Item Four') Parent(#PMNU_1) 
   Set Com(#Items<5>) Caption('Item Five') Parent(#PMNU_1) 
ENDROUTINE  
 
EVTROUTINE HANDLING(#Items<>.Click) COM_SENDER(#AnyOfTheGroup)
   Change #M_Caption #AnyOfTheGroup.Caption
ENDROUTINE