
So that you can respond to events, you must first register the events for the control instance and consequently on the frontend. When the control is set to its initial state, the SAPGUI filters all events.
In this exercise, you register event DBLCLICK in the textedit control as an application event. Initially, you only want to display a text for the event triggered on the screen.
Declare the following variables in your main program: a) An internal table for the events you want to register, and b) a structure for one line of this table:
DATA events TYPE cntl_simple_events. DATA wa_events TYPE cntl_simple_event.
Insert the following lines of code into the IF block of the PBO module after the control is created.
Assign the static attribute for event DBLCLICK to field eventid:
wa_events-eventid = cl_gui_textedit=>event_double_click.
Using field appl_event , define the event as an application event:
wa_events-appl_event = 'X'.
Append this line to your internal table events, and pass this table to the textedit control using method set_registered_events:
APPEND wa_events TO events. CALL METHOD editor->set_registered_events EXPORTING events = events.
This method is provided by all controls. Depending on the control, there may be additional methods that you can use to register individual events.
In the PAI module, call method DISPATCH in the last query of the CASE statement:
WHEN OTHERS. CALL METHOD CL_GUI_CFW=>DISPATCH.
Activate and start your program.
Check Your Work
If you double-click in the text window of the editor, the text of text element text-002 is displayed on the screen.
Discussion
Registering event DBLCLICK means that the frontend transfers double-clicks of the user to the backend by means of the OK code. Since you registered this event as an application event, the Control Framework processes the PAI module of the screen concerned. You use method DISPATCH to determine when the event is triggered by ABAP Objects. The runtime environment then executes method CATCH_DBLCLICK and continues processing after the DISPATCH call.
The DISPATCH call is globally valid for all control instances of the main program created.
Advantages
You determine yourself when the event handler method is to be called. To do this, you use method DISPATCH.
Since the system processes the PAI module, the field transport between the screen and the application server has already taken place. This means you can access screen fields in the event handler method.
Disadvantages
When you use application events, you may encounter problems when you check entries using the FIELD or CHAIN statement. If entries are not correct, the user may be forced to re-enter the data. However, in doing this, the user may trigger other events in the control. This way, events could be lost.
Using method DISPATCH several times does not provide a solution to this problem since the event handler methods are designed to be called only once.