Creating a Control: SAP Picture Example


The following process applies to all SAP custom controls. The programming examples use the SAP Picture Control. However, to apply the example to other controls, you would only have to change the name of the control class.

The example also assumes that you are using the custom control in a Custom Container. The SAP Container documentation contains details of further scenarios.

Process Flow

Create the Instance

  1. Define a reference variable for the Custom Container in which you want to place the custom control ( see SAP Container).

    DATA container TYPE REF TO cl_gui_custom_container.

  2. Define a reference variable for the SAP Picture:

    DATA picture TYPE REF TO cl_gui_picture.

  3. Create the Custom Container. You must already have created the area 'CUSTOM' for the Custom Container in the Screen Painter. When you create the container, you must also specify its lifetime (see constructor).

    CREATE OBJECT container
        EXPORTING container_name = 'CUSTOM'

                  lifetime       = lifetime.

  4. Create the SAP Picture Control. You can also specify a lifetime for the SAP Picture, but it must not be longer than that of its container.

    CREATE OBJECT picture
         EXPORTING parent  = container

                  lifetime = lifetime.

    Register the Events

  5. There are three steps: Registering the events with the Control Framework, defining a handler method, and registering the hander method. These steps are explained under Registering and Processing Events.

    Use the Control

  6. These steps are control-specific and therefore not described here.

    Destroy the Control

    The lifetime management is normally responsible for destroying any controls you use. However, the following two steps allow you to destroy the control yourself:

  7. Use the method free to destroy the Custom Control at the frontend. If you no longer need the control container, release it as well:

    CALL METHOD picture->free
          EXCEPTIONS cntl_error        = 1
                     cntl_system_error = 2.
    CALL METHOD container->free
          EXCEPTIONS cntl_error        = 1
                     cntl_system_error = 2.

    Pay careful attention to the sequence in which you destroy controls at the frontend. When you destroy a container, all controls in it are automatically destroyed as well. If you have already destroyed a control and try to destroy it again, an error occurs. You can check whether a control has already been destroyed using the method is_alive.

  8. Delete the reference variables to the custom control and the control container.