SAP ABAP OBJECTS Tutorials

Example 1: Creating the TextEdit control

This is a simple example of how to implement a text edit control.

Steps

Create a report
In the start of selection event add: SET SCREEN ‘100’.
Create screen 100
Place a custom control on the screen by choosing the custom control icon which can be recognized by the letter ‘C’, and give it the name MYCONTAINER1.
To be able to exit the program, add a pushbutton with the function code EXIT.
In the elements list enter the name OK_CODE for the element of type OK.

The code

REPORT sapmz_hf_controls1 .

CONSTANTS:

line_length TYPE i VALUE 254.

DATA: ok_code LIKE sy-ucomm.

DATA:

* Create reference to the custom container

custom_container TYPE REF TO cl_gui_custom_container,

* Create reference to the TextEdit control

editor TYPE REF TO cl_gui_textedit,

repid LIKE sy-repid.

START-OF-SELECTION.

SET SCREEN ‘100’.

*———————————————————————*

*       MODULE USER_COMMAND_0100 INPUT                                *

*———————————————————————*

MODULE user_command_0100 INPUT.

CASE ok_code.

WHEN ‘EXIT’.

LEAVE TO SCREEN 0.

ENDCASE.

ENDMODULE.                 ” USER_COMMAND_0100  INPUT

*&———————————————————————*

*&      Module  STATUS_0100  OUTPUT

*&———————————————————————*

MODULE status_0100 OUTPUT.

* The TextEdit control shoul only be initialized the first time the

* PBO module executes

IF editor IS INITIAL.

repid = sy-repid.

*   Create obejct for custom container

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

others                      = 6

.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE ‘I’ NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

*   Create obejct for the TextEditor control

CREATE OBJECT editor

EXPORTING

wordwrap_mode          =

cl_gui_textedit=>wordwrap_at_fixed_position

wordwrap_position      = line_length

wordwrap_to_linebreak_mode = cl_gui_textedit=>true

parent                  = custom_container

EXCEPTIONS

error_cntl_create      = 1

error_cntl_init        = 2

error_cntl_link        = 3

error_dp_create        = 4

gui_type_not_supported = 5

others                 = 6

.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE ‘I’ NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

ENDIF.

ENDMODULE.                 ” STATUS_0100  OUTPUT

The result

Example 2: Event handling – Application event

There are 2 types of events:

System events. These events are triggerede irrespective of the screen flow-logic.
Application events. The PAI module is processed after an event. The method CL_GUI_CFW=>DISPATCH must be called to initiate event handling

In this example an application event is added to the program in example 1. New code is marked with red.

Steps:

Create an input/output field on screen 100, where the event type can be output. Name it EVENT_TYPE

The code:

REPORT sapmz_hf_controls1 .

CONSTANTS:

line_length TYPE i VALUE 254.

DATA: ok_code LIKE sy-ucomm.

DATA:

* Create reference to the custom container

custom_container TYPE REF TO cl_gui_custom_container,

* Create reference to the TextEdit control

editor TYPE REF TO cl_gui_textedit,

repid LIKE sy-repid.

**********************************************************************

* Impmenting events

**********************************************************************

DATA:

event_type(20) TYPE c,

* Internal table for events that should be registred

i_events TYPE cntl_simple_events,

* Structure for oneline of the table

wa_events TYPE cntl_simple_event.

*———————————————————————*

*       CLASS lcl_event_handler DEFINITION

*———————————————————————*

CLASS lcl_event_handler DEFINITION.

PUBLIC SECTION.

CLASS-METHODS:

catch_dblclick FOR EVENT dblclick

OF cl_gui_textedit IMPORTING sender.

ENDCLASS.

CLASS lcl_event_handler IMPLEMENTATION.

METHOD catch_dblclick.

event_type = ‘Event DBLCLICK raised’.

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

CLEAR wa_events. refresh i_events.

SET SCREEN ‘100’.

*———————————————————————*

*       MODULE USER_COMMAND_0100 INPUT                                *

*———————————————————————*

MODULE user_command_0100 INPUT.

CASE ok_code.

WHEN ‘EXIT’.

LEAVE TO SCREEN 0.

WHEN OTHERS.

*   Call the Dispacth method to initiate application event handling

call method cl_gui_cfw=>Dispatch.

ENDCASE.

ENDMODULE.                 ” USER_COMMAND_0100  INPUT

*&———————————————————————*

*&      Module  STATUS_0100  OUTPUT

*&———————————————————————*

MODULE status_0100 OUTPUT.

* The TextEdit control shoul only be initialized the first time the

* PBO module executes

IF editor IS INITIAL.

repid = sy-repid.

*   Create obejct for custom container

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

others                      = 6

.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE ‘I’ NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

*   Create obejct for the TextEditor control

CREATE OBJECT editor

EXPORTING

wordwrap_mode          =

cl_gui_textedit=>wordwrap_at_fixed_position

wordwrap_position      = line_length

wordwrap_to_linebreak_mode = cl_gui_textedit=>true

parent                  = custom_container

EXCEPTIONS

error_cntl_create      = 1

error_cntl_init        = 2

error_cntl_link        = 3

error_dp_create        = 4

gui_type_not_supported = 5

others                 = 6

.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE ‘I’ NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

*   Link the event handler method to the event and the

*   TextEdit control

SET HANDLER lcl_event_handler=>catch_dblclick FOR editor.

*   Register the event in the internal table i_events

wa_events-eventid = cl_gui_textedit=>event_double_click.

wa_events-appl_event = ‘X’. “This is an application event

append wa_events to i_events.

*   Pass the table to the TextEdit control using method

*   set_registred_events

call method editor->set_registered_events

exporting events = i_events.

ENDIF.

ENDMODULE.                 ” STATUS_0100  OUTPUT

Result:

When you double click on the TextEdit control, the input/ouput field should show the text: Event DBLCLICK