Exercise 4: Responding to an Event
Usage
In the previous exercise, you have registered an event. You have used the
SET HANDLER statement to link the event handler method to the event. To respond to this event, you only need to extend the event handler method.In this exercise, you implement a function that converts a line into a comment line when the line is double-clicked. If the line is a comment line already, the comment marker at the beginning of the line is deleted. Implement this function by retrieving additional control information in the event handler method and using methods to trigger control actions.
Procedure
Add the following code blocks to method
catch_dblclick before set_new_ok_code is called:DATA: FROM_LINE TYPE I,
FROM_POS TYPE I,
TO_LINE TYPE I,
TO_POS TYPE I.
CALL METHOD SENDER->GET_SELECTION_POS
IMPORTING
FROM_LINE = FROM_LINE
FROM_POS = FROM_POS
TO_LINE = TO_LINE
TO_POS = TO_POS.
IF NOT g_loaded IS INITIAL.
CALL METHOD SENDER->GET_TEXT_AS_R3TABLE
importing
TABLE = MYTABLE.
ENDIF.
READ TABLE MYTABLE INDEX FROM_LINE INTO TEXTSTRUCT.
IF SY-SUBRC = 0.
IF TEXTSTRUCT+0(1) CS '*'.
SHIFT TEXTSTRUCT.
ELSEIF TEXTSTRUCT+0(1) NS '*'.
SHIFT TEXTSTRUCT RIGHT.
TEXTSTRUCT+0(1) = '*'.
ENDIF.
MODIFY MYTABLE FROM TEXTSTRUCT INDEX FROM_LINE.
CALL METHOD SENDER->SET_TEXT_AS_R3TABLE
EXPORTING TABLE = MYTABLE
EXCEPTIONS
OTHERS = 1.
ENDIF.
CALL METHOD cl_gui_cfw=>FLUSH.
IF SY-SUBRC NE 0.
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
TITEL = REPID
TXT2 = sy-subrc
TXT1 = 'Method CATCH_DBLCLICK: Error in Flush!'(602).
ENDIF.
Check Your Work
To display the generated text of the internal table in the text window, click on pushbutton
Import . If you now double-click a line, an asterisk (' * ') appears at the beginning of that line, and the text is indented to the right. If you double-click the line again, the asterisk is removed.Discussion
Method
get_text_as_r3table loads the text in the text window of the control into internal table mytable . In the next step, this table is modified. However, from a chronological point of view, the method should not be executed before flush time. This would mean that the modification is carried out prematurely. Why does the program behave correctly nevertheless?The reason is that another control is used when large volumes of data are transferred by control methods. This control is the Data Provider. Currently, the Data Provider implicitly triggers another flush to transfer the data.

You can detect any implicit flush in the trace mode. This mode is described in lesson 4 in
However, it is not to your disadvantage to define another flush after method
get_text_as_r3table . If a flush is called and no control methods are called before, the Control Framework does not perform an RFC.
SAP recommends you to use an additional flush call and not to rely on an implicit flush. This ensures that your programs are more robust. If the implicit flush is eliminated in later versions of the Data Provider, this will not effect your existing programs.
The next lesson describes in detail how flush calls are used correctly.