Exercise 3: Calling Methods of the Control

Use

Once you have created an instance of a control, you affect its behavior by means of methods. These methods help you to use the functions of the control and determine its properties. In order to execute control methods, you must normally transfer them to the frontend.

In this exercise, you implement a function that loads a text from an internal table into the text window.

Procedure

  1. To generate a text, declare an internal table and a work area globally in your main program. Declare also global flag g_loaded:

    TYPES: begin of mytable_line,
              line(line_length) type C,
              end of mytable_line.
    DATA: MYTABLE TYPE TABLE OF MYTABLE_LINE,
          TEXTSTRUCT TYPE MYTABLE_LINE,
          g_loaded TYPE C.
  2. Define a text element and have your internal table filled with text in the PBO module. As the system should do this only once, include your statements in the IF block for creating the control:

    DO 20 TIMES.
       WRITE TEXT-001 TO TEXTSTRUCT-LINE.
       APPEND TEXTSTRUCT TO MYTABLE.
    ENDDO.
    
  3. To make the new function accessible, define another pushbutton on screen 100 that has the following properties:

    • Function code: IMP

    • Text: Import

    • icon: ICON_IMPORT

  4. In the PAI module, determine the function code in the existing CASE statement, and create subroutine LOAD_TAB:

    WHEN 'IMP'.
          PERFORM LOAD_TAB.
    
  5. To load the text of the internal table into the editor, call method set_text_as_r3table in your subroutine. Flag g_loaded tells you that the text has been loaded:

    FORM LOAD_TAB.
       call method editor->set_text_as_r3table
                 exporting table = mytable
          exceptions
              others = 1.
       if sy-subrc ne 0.
           CALL FUNCTION 'POPUP_TO_INFORM'
                 EXPORTING
                      TITEL = repid
                      TXT2 = ' '
                      TXT1 = 'Error in set_text_as_r3table'(600).
       else.
          g_loaded = 'X'.
       endif.
       CALL METHOD CL_GUI_CFW=>FLUSH.
       IF SY-SUBRC NE 0.
          CALL FUNCTION 'POPUP_TO_INFORM'
              EXPORTING
                    TITEL = REPID
                    TXT2 = sy-subrc
                    TXT1 = 'Form LOAD_TAB: Error in FLUSH'(601).
       ENDIF.
    ENDFORM.                   " LOAD_TAB
    
  6. Activate your new objects, and start your program.

Check Your Work

If you click the Import pushbutton you created, twenty lines appear that display the text of text element TEXT-001. If not, you probably have forgotten to create the text element.