Show TOC

Procedure documentationExercise 2: Creating a Control and its Container Locate this document in the navigation structure

Prerequisites

In Exercise 1, you created a screen for your main program on which you placed an area for a Custom Container Control. In the next step, you create this control and link it to a TextEdit Control.

Procedure

  1. Declare the following variables in your main program:

    • a reference variable to the class of the custom container control

    • a reference variable to the class of the textedit control, and

    • a variable that contains the ID of the current report:

    Syntax Syntax

    1. DATA: custom_container TYPE REF TO cl_gui_custom_container,
            editor TYPE REF TO cl_gui_textedit,
            repid LIKE sy-repid.
      
    End of the source code.
  2. Create constant line_length that determines the length of lines in the text window of the textedit control:

    Syntax Syntax

    1. CONSTANTS: line_length TYPE I VALUE 256.
      
    End of the source code.
  3. Create PBO module status_100 in the flow logic of screen 100.

  4. In the PBO module, instantiate the custom container control and link it to the MYCONTAINER1 custom control area:

    Syntax Syntax

    1. IF EDITOR IS INITIAL.
         repid = sy-repid.
         CREATE OBJECT custom_container
            EXPORTING
               CONTAINER_NAME = 'MYCONTAINER1'
            EXCEPTIONS
               CNTL_ERROR = 1
               CNTL_SYSTEM_ERROR = 2
               CREATE_ERROR = 3
               LIFETIME_ERROR = 4
               LIFETIME_DYNPRO_DYNPRO_LINK = 5.
      
    End of the source code.
  5. In the PBO module, instantiate the textedit control and specify the container control you have just created as the parent parameter:

    Syntax Syntax

    1. CREATE OBJECT editor
            EXPORTING
               parent = custom_container
               WORDWRAP_MODE = CL_GUI_TEXTEDIT=>WORDWRAP_AT_FIXED_POSITION
               WORDWRAP_POSITION = line_length
               WORDWRAP_TO_LINEBREAK_MODE = CL_GUI_TEXTEDIT=>TRUE.
      ENDIF.                 "editor is initial
      
    End of the source code.
  6. Activate your main program.

Check Your Work

Start your program. You can now see a text editor at the screen position where you created the container. Nine icons are displayed above the input field of the editor.

If you do not see all these icons on your screen, you must enlarge the container in the Screen Painter.

The textedit control provides basic functions for editing text. You can:

  • Cut, copy and paste text

  • Undo actions

  • Search and replace text

  • Load and save local files

You can access these functions either via the tool bar or via the context menu (by clicking the right mouse button). Drag and drop functions are also available. You can:

  • Load texts by dragging text file icons (no shortcuts) into the text window.

  • Move text selected in the text window with the right mouse button.

Discussion
Attributes

The CREATE OBJECT statement calls the constructor of class cl_gui_textedit. The constructor determines some initial properties of the instantiated object when it is created. In this exercise, you set two attributes using constants of the class. The WORDWRAP_MODE attribute determines if and how words are wrapped. The WORDWRAP_TO_LINEBREAK_MODE attribute determines if the system converts automatic wrapping into an actual line break when the text is saved in the SAP system.

Note Note

You can display methods (including constructors), attributes, and events of a class in the Class Builder (by using transaction SE24 or choosing   Development   Class Builder   from within the initial screen of the ABAP Workbench).

End of the note.
Time of Creation

All methods that operate on a control must be transferred to the frontend by means of a Remote Function Call (RFC). By calling method CL_GUI_CFW=>FLUSH, you can explicitly determine when this takes place.

Note Note

At the end of each PBO module, this method is implicitly called after the screen fields are transported. This ensures that the methods are transferred to the frontend before the screen is set up (see also Synchronization of the Automation Queue in the Control Framework documentation).

End of the note.

The actual parameters of control methods must be valid at the time of transfer. The values of global system variables change frequently since they are also used in methods and function modules called. Consequently, they are not suitable as actual parameters.

This is why the repid variable rather than the sy-repid system variable was passed in the constructor method after it was assigned the current value of the system field. The repid variable is only visible in the main program and thus remains stable.

Lifetime

Lifetime All statements are included in an IF block to ensure that the control is instantiated only once and not each time the PBO module is called.

The lifetime of a control depends on the mode that you can specify in the constructor using parameter LIFETIME. In the default setting (IMode mode), controls that you create persist until:

  • The internal mode is destroyed in which it was created.

  • The container control is destroyed in which it is embedded.

  • You call the FREE method of the instance created.

    Note Note

    For more information on the lifetime of controls, see Lifetime Management in the Control Framework documentation.

    End of the note.