Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

USE DIGITAL TIMER IN SCREEN PAINTER SCREEN.

Former Member
0 Likes
7,055

Hello Guyz,

I am developing online examination system in ABAP using screen painter for taking online exams. At main screen i want to show decrementing timer for time duration of test whenever i click start test button. for e.g if duration of test is  20 minutes then it show timer starting from 20 minutes and it should decrement it by 1 second & whenever it reaches 0 , it should exit the whole program.

I have developed the simple executable program to decrease the timer but i do not know how to use it screens. Please help me attaching timer to screen.

Here is sample code which i have created for decrementing timer.

PARAMETERS : p_time type t DEFAULT 1200.

*Class to handle the FINSHED Event Raised by CL_GUI_TIMER
CLASS my DEFINITION.
PUBLIC SECTION.
METHODS : run_handler FOR EVENT finished OF cl_gui_timer.
ENDCLASS.
DATA timer TYPE REF TO cl_gui_timer.
DATA myh TYPE REF TO my.

CLASS my IMPLEMENTATION.
METHOD run_handler.
CALL METHOD timer->run.
call method CL_GUI_CFW=>SET_NEW_OK_CODE
EXPORTING
new_code = 'OK_CODE'.
ENDMETHOD.
ENDCLASS.

at SELECTION-SCREEN OUTPUT.
p_time = P_time - 1.

CREATE OBJECT timer.
CREATE OBJECT myh.
timer->interval = '1'.
CALL METHOD timer->run.
SET HANDLER myh->run_handler FOR ALL INSTANCES.

plelase help guyz , i need it urgently.

Thanks & regards ,

kapil.

1 ACCEPTED SOLUTION
Read only

RaymondGiuseppi
Active Contributor
0 Likes
6,283

Usually I use some code for auto-refresh looking like

* TOP include
CLASS lcl_event_receiver DEFINITION.
   PUBLIC SECTION.
     METHODS:
       on_finished FOR EVENT finished OF cl_gui_timer.
*        IMPORTING sender.
   PRIVATE SECTION.
ENDCLASS.                    "lcl_event_receiver DEFINITION

CLASS lcl_event_receiver IMPLEMENTATION.
   METHOD: on_finished.
*    CASE sender.
     CALL METHOD cl_gui_cfw=>set_new_ok_code
       EXPORTING
         new_code = 'CLOCK'.
*    ENDCASE.
   ENDMETHOD.                           " on_finished
ENDCLASS.                    "lcl_event_receiver IMPLEMENTATION

DATA: go_alarm TYPE REF TO lcl_event_receiver,
       go_clock TYPE REF TO cl_gui_timer.

* PBO module of screen dynpro
IF go_clock IS INITIAL.
   CREATE OBJECT: go_clock.
ENDIF.
IF go_alarm IS INITIAL.
   CREATE OBJECT: go_alarm.
   SET HANDLER go_alarm->on_finished FOR go_clock.
ENDIF.
go_clock->interval = 1.
CALL METHOD go_clock->run.

You should not decrement time in the event, as user can trigger PAI/PBO cycles too, so better store start time before first screen with a GET RUN TIME, and another statement in PBO to get curret time and perform a subtraction.

Regards,

Raymond

*&---------------------------------------------------------------------*

*& Report Y_TIMER
*&---------------------------------------------------------------------*
*& Author: South Korea SAP Developer.
*&---------------------------------------------------------------------*
REPORT Y_TIMER.
*&---------------------------------------------------------------------*
*& TIMER CLASS.
*&---------------------------------------------------------------------*
CLASS LCL_RECEIVER DEFINITION.
PUBLIC SECTION.
METHODS:
HANDLE_FINISHED FOR EVENT FINISHED OF CL_GUI_TIMER.
ENDCLASS.
CLASS LCL_RECEIVER IMPLEMENTATION.
METHOD HANDLE_FINISHED.
PERFORM HANDLE_TIMER_RUN.
ENDMETHOD.
ENDCLASS.
DATA:
RECEIVER TYPE REF TO LCL_RECEIVER,
TIMER TYPE REF TO CL_GUI_TIMER.
*&---------------------------------------------------------------------*

DATA:
G_TIMER_COUNTER TYPE I,
G_SRT_TIME TYPE SY-UZEIT,
G_CUR_TIME TYPE SY-UZEIT,
G_EST_TIME TYPE SY-UZEIT.


START-OF-SELECTION.
*
G_SRT_TIME = G_CUR_TIME = SY-UZEIT.
CALL SCREEN 100 STARTING AT 10 2 ENDING AT 20 4.
*
FORM HANDLE_TIMER_RUN.

ADD 1 TO G_TIMER_COUNTER.
G_CUR_TIME = SY-UZEIT.
G_EST_TIME = G_CUR_TIME - G_SRT_TIME.

CALL METHOD CL_GUI_CFW=>SET_NEW_OK_CODE
EXPORTING
NEW_CODE = 'ENTR'.

CALL METHOD TIMER->RUN.

ENDFORM.
*
MODULE PBO OUTPUT.
SET PF-STATUS 'POPU' OF PROGRAM 'SAPLSPO1'.
IF TIMER IS INITIAL.
CREATE OBJECT TIMER.
CREATE OBJECT RECEIVER.
TIMER->INTERVAL = 1.
SET HANDLER RECEIVER->HANDLE_FINISHED FOR TIMER.
CALL METHOD TIMER->RUN.
ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT
*
MODULE PAI INPUT.
CASE SY-UCOMM.
WHEN 'ENTR'.
CALL METHOD TIMER->RUN.
CLEAR SY-UCOMM.
WHEN 'CANC'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT

SCREEN 0100

18 REPLIES 18
Read only

Former Member
0 Likes
6,282

Good question, I'm also interested to know.  Is data binding like that even possible via SAPGUI?

Read only

Former Member
0 Likes
6,279

Timer can be decremented on PBO, and screen can show the timer value in read-only mode.

Screen elements are:

Screen flow logic PBO has MODULE status_0100.

Here is the code snippet.

DATA p_time TYPE t VALUE 10.

*Class to handle the FINSHED Event Raised by CL_GUI_TIMER
CLASS my DEFINITION.
  PUBLIC SECTION.
    METHODS : run_handler FOR EVENT finished OF cl_gui_timer.
ENDCLASS.                    "my DEFINITION
DATA timer TYPE REF TO cl_gui_timer.
DATA myh TYPE REF TO my.

*----------------------------------------------------------------------*
*       CLASS my IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS my IMPLEMENTATION.
  METHOD run_handler.
    CHECK p_time IS NOT INITIAL.
    CALL METHOD timer->run.
    CALL METHOD cl_gui_cfw=>set_new_ok_code
      EXPORTING
        new_code = 'OK_CODE'.
  ENDMETHOD.                    "run_handler
ENDCLASS.                    "my IMPLEMENTATION

*at SELECTION-SCREEN OUTPUT.
*p_time = P_time - 1.

START-OF-SELECTION.
  CREATE OBJECT timer.
  CREATE OBJECT myh.
  timer->interval = '1'.
  CALL METHOD timer->run.
  SET HANDLER myh->run_handler FOR ALL INSTANCES.

  CALL SCREEN 100.
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
*  SET PF-STATUS 'xxxxxxxx'.
*  SET TITLEBAR 'xxx'.
  p_time = p_time - 1.
ENDMODULE.                 " STATUS_0100  OUTPUT

Note: Your timer does not stop when timer reaches zero. I have added an exit condition in method implementation.

Read only

0 Likes
6,279

hello Manish i have implement ur code. but its not working.please help me.. ur code seems to be fine but m not able to detect where the error is. its not showing any error while activation. still its not showing any progress.

Please help me out ur help will be aapreciated.

thanks & regards,

kapil

Read only

0 Likes
6,279

The type of p_time variable is t in program, and when i created p_time I/O field in screen, its default type was char. It didn't work initially, and started working when i manually changed screen field type to TIMS. Maybe this is the issue at your end.

If not, I would like you to post code, screen flow logic and screenshot of screen elements.

Read only

0 Likes
6,279

Yes Manish , issue was exactly as u are saying.. i detected it on my own but wasn't able to post the result. But one more thing it showing the clock on I/O filed but its not decrementing automatically . Its showing only 9 i.e only one decrementing value.please put some light on this.

Thanks & Regards

kapil

Read only

0 Likes
6,279

Could you paste the code and screen related screenshots?

It would be easier that way to pinpoint the issue. Same code is working for me.

Read only

0 Likes
6,279

Manish , I guess ur code is ok. Its displaying timer in I/O filed but its not decrementing automatically , its decrementing whenever it click push button START. Whether it should decrement automatically by 1s. or atleast whenever  i click the push button it should show the current state of time.

For e.g suppose i executes my program , it should start decrementing time on screen  or if its cant happen it should show current status after push button. i.e how much time is left after clicking button.

Here is code & screen shot

DATA p_time TYPE t VALUE 10.

*Class to handle the FINSHED Event Raised by CL_GUI_TIMER
CLASS my DEFINITION.
   PUBLIC SECTION.
     METHODS : run_handler FOR EVENT finished OF cl_gui_timer.
ENDCLASS.                    "my DEFINITION
DATA timer TYPE REF TO cl_gui_timer.
DATA myh TYPE REF TO my.

*----------------------------------------------------------------------*
*       CLASS my IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS my IMPLEMENTATION.
   METHOD run_handler.
     CHECK P_Time IS NOT INITIAL.
     CALL METHOD timer->run.
     CALL METHOD cl_gui_cfw=>set_new_ok_code
       EXPORTING
         new_code = 'save_code'.
   ENDMETHOD.                    "run_handler
ENDCLASS.                    "my IMPLEMENTATION

*at SELECTION-SCREEN OUTPUT.
*p_time = P_time - 1.

START-OF-SELECTION.
   CREATE OBJECT timer.
   CREATE OBJECT myh.
   timer->interval = '1'.
   CALL METHOD timer->run.
   SET HANDLER myh->run_handler FOR ALL INSTANCES.

   CALL SCREEN 3000.



MODULE STATUS_3000 OUTPUT.
   SET PF-STATUS 'STATUS'.
*  SET TITLEBAR 'xxx'.


p_time = p_time - 1.

case p_time.
when 0.
leave PROGRAM.
ENDCASE.

ENDMODULE.                 " STATUS_3000  OUTPUT

screen shots

After Ist execution , it shows only one decremented timr i.e 9sec & dont decrement at all.

Now its got decrement whenever i clicks start button  otherwise its keep still



Read only

0 Likes
6,279

I believe you have not uncommented the PBO code in screen flow logic.

My Screen flow logic is:

PROCESS BEFORE OUTPUT.
MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
* MODULE USER_COMMAND_0100.

Yours would probably be:

PROCESS BEFORE OUTPUT.
* MODULE STATUS_3000.
*
PROCESS AFTER INPUT.
* MODULE USER_COMMAND_3000.

So, your screen gets called, timer get triggered every 1 second, Module Status_3000 is present in code, but not really linked to screen 3000 flow logic. As a result, value does not get decremented.

Try double clicking on module and see if it takes your to screen.

Try double clicking on module call in screen flow logic and see if it navigated to correct module code.

Correct me if I am wrong.

Read only

0 Likes
6,279

No Mine is also uncommented. u can see in screenshot.

Read only

0 Likes
6,279

Well same code is working for me. The counter is decrementing, and screen is updating every second automatically.

My last suggestion would be to paste the code in fresh program, create screen by double clicking, add elements like screenshot, uncomment PBO module and double click to create, and activate.

Read only

0 Likes
6,279

Ok i will try to do the same but i guess problem is that decrementing is associated with 'OK_CODE'  that why its decrement only when i clicks any button is clicked as shown in code.

CLASS my IMPLEMENTATION.
  METHOD run_handler.
    CHECK p_time IS NOT INITIAL.
    CALL METHOD timer->run.
    CALL METHOD cl_gui_cfw=>set_new_ok_code
      EXPORTING
        new_code = 'OK_CODE'.
  ENDMETHOD.                    "run_handler
ENDCLASS.                    "my IMPLEMENTATION

so , whats ur take on this , is the way am thinking is correct. please help me if u have any knowledge on this i have to submit this project by tommorrow only.

Read only

0 Likes
6,279

one more thing i want to  convey that i want timer to be start when i click on start button not by default calling of screen.

Read only

former_member188282
Active Participant
0 Likes
6,279

Hi Kapil,

Try with changing the

new_code = 'OK_CODE'. to new_code = ‘REFR’.

Check the below link for more info.

http://sapignite.com/how-to-make-a-timer-in-sap-abap/

Regards,

Rajesh

Read only

RaymondGiuseppi
Active Contributor
0 Likes
6,284

Usually I use some code for auto-refresh looking like

* TOP include
CLASS lcl_event_receiver DEFINITION.
   PUBLIC SECTION.
     METHODS:
       on_finished FOR EVENT finished OF cl_gui_timer.
*        IMPORTING sender.
   PRIVATE SECTION.
ENDCLASS.                    "lcl_event_receiver DEFINITION

CLASS lcl_event_receiver IMPLEMENTATION.
   METHOD: on_finished.
*    CASE sender.
     CALL METHOD cl_gui_cfw=>set_new_ok_code
       EXPORTING
         new_code = 'CLOCK'.
*    ENDCASE.
   ENDMETHOD.                           " on_finished
ENDCLASS.                    "lcl_event_receiver IMPLEMENTATION

DATA: go_alarm TYPE REF TO lcl_event_receiver,
       go_clock TYPE REF TO cl_gui_timer.

* PBO module of screen dynpro
IF go_clock IS INITIAL.
   CREATE OBJECT: go_clock.
ENDIF.
IF go_alarm IS INITIAL.
   CREATE OBJECT: go_alarm.
   SET HANDLER go_alarm->on_finished FOR go_clock.
ENDIF.
go_clock->interval = 1.
CALL METHOD go_clock->run.

You should not decrement time in the event, as user can trigger PAI/PBO cycles too, so better store start time before first screen with a GET RUN TIME, and another statement in PBO to get curret time and perform a subtraction.

Regards,

Raymond

Read only

0 Likes
6,279

Hello Raymond ,

I didn't understand ur answer as am new to classes. Can u elaborate with reference to my code  and guide where to use which code. Ur help will be appreciated as i have to submit the project by tomorrow only.

Read only

Former Member
0 Likes
6,279

Thanks Manish & Raymond , you guyz helped me a lot.

With the help of ur code raymond my timer is auto decrementing.

Thanks a ton. You guyz just saved me.

Thanks & Regards,

kapil

Read only

0 Likes
6,279

Hello Kapil,

Even I'm working on developing online exam in ABAP screen painter.

Could you please share me the abap code.

Thanks.

Read only

joungeun
Member
0 Likes
6,279

*&---------------------------------------------------------------------*

*& Report Y_TIMER
*&---------------------------------------------------------------------*
*& Author: South Korea SAP Developer.
*&---------------------------------------------------------------------*
REPORT Y_TIMER.
*&---------------------------------------------------------------------*
*& TIMER CLASS.
*&---------------------------------------------------------------------*
CLASS LCL_RECEIVER DEFINITION.
PUBLIC SECTION.
METHODS:
HANDLE_FINISHED FOR EVENT FINISHED OF CL_GUI_TIMER.
ENDCLASS.
CLASS LCL_RECEIVER IMPLEMENTATION.
METHOD HANDLE_FINISHED.
PERFORM HANDLE_TIMER_RUN.
ENDMETHOD.
ENDCLASS.
DATA:
RECEIVER TYPE REF TO LCL_RECEIVER,
TIMER TYPE REF TO CL_GUI_TIMER.
*&---------------------------------------------------------------------*

DATA:
G_TIMER_COUNTER TYPE I,
G_SRT_TIME TYPE SY-UZEIT,
G_CUR_TIME TYPE SY-UZEIT,
G_EST_TIME TYPE SY-UZEIT.


START-OF-SELECTION.
*
G_SRT_TIME = G_CUR_TIME = SY-UZEIT.
CALL SCREEN 100 STARTING AT 10 2 ENDING AT 20 4.
*
FORM HANDLE_TIMER_RUN.

ADD 1 TO G_TIMER_COUNTER.
G_CUR_TIME = SY-UZEIT.
G_EST_TIME = G_CUR_TIME - G_SRT_TIME.

CALL METHOD CL_GUI_CFW=>SET_NEW_OK_CODE
EXPORTING
NEW_CODE = 'ENTR'.

CALL METHOD TIMER->RUN.

ENDFORM.
*
MODULE PBO OUTPUT.
SET PF-STATUS 'POPU' OF PROGRAM 'SAPLSPO1'.
IF TIMER IS INITIAL.
CREATE OBJECT TIMER.
CREATE OBJECT RECEIVER.
TIMER->INTERVAL = 1.
SET HANDLER RECEIVER->HANDLE_FINISHED FOR TIMER.
CALL METHOD TIMER->RUN.
ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT
*
MODULE PAI INPUT.
CASE SY-UCOMM.
WHEN 'ENTR'.
CALL METHOD TIMER->RUN.
CLEAR SY-UCOMM.
WHEN 'CANC'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT

SCREEN 0100