Google Search - Blog...........

ABAP - Calculator Interface In Screen Using Push Button & Text Box Controls In Screen Editor.

Design calculator interface in screen using push button and text box controls in screen editor.
The copy the relevant code as follows to the respective events.

include mzrs_calctop . " global Data include mzrs_calco01 . " PBO-Modules
include mzrs_calci01 . " PAI-Modules
include mzrs_calcf01 . " FORM-Routines type-pools:slis.
*&---------------------------------------------------------------------*
*& Include MZRS_CALCTOP Module pool SAPMZRS_CALC
*&---------------------------------------------------------------------*data: ok_code type sy-ucomm,
save_code type sy-ucomm.
data: var1 type i,
var2 type i,
res type i value 0,
temp type i.
data oper.
data text type string.
data on type i value 0.
data: r_exc type ref to cx_root.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
module status_0100 output.
set pf-status 'ZRS_MENU '.
set titlebar 'CALCULATOR'.
if on = 0.
loop at screen.
if screen-group1 = 'G1'.
screen-active = 0.
modify screen.
endif.
endloop.
endif.
endmodule. " STATUS_0100 OUTPUT

*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
module user_command_0100 input.
save_code = ok_code.
clear ok_code.
data op type i value 0.
case save_code.
when 'P1'.
if op = 0.
var1 = ( res * 10 ) + 1.
res = var1.
else.
var2 = ( res * 10 ) + 1.
res = var2.
endif.
when 'P2'.
if op = 0.
var1 = ( res * 10 ) + 2.
res = var1.
else.
var2 = ( res * 10 ) + 2.
res = var2.
endif.

* similarly put for other push buttons p3 to p9.
when 'P0'.
if op = 0.
var1 = ( res * 10 ) + 0.
res = var1.
else.
var2 = ( res * 10 ) + 0.
res = var2.
endif.

when 'PA'.
op = 1.
res = 0.
oper = '+'.
when 'PS'.
op = 1.
res = 0.
oper = '-'.
when 'PM'.
op = 1.
res = 0.
oper = '*'.
when 'PD'.
op = 1.
res = 0.
oper = '/'.
when 'PEQ'.
case oper.
when '+'.
clear: res, oper.
res = var1 + var2.
when '-'.
clear: res, oper.
if var1 ge var2.
res = var1 - var2.
else.
res = var2 - var1.
res = res * -1.
endif.
when '*'.
clear: res, oper.
try.
res = var1 * var2.
catch cx_sy_arithmetic_overflow into r_exc.
text = r_exc->get_text( ).
message text type 'I'.
endtry.
when '/'.
clear: res, oper.
if var1 ne 0.
res = var1 / var2.
else.
message 'Divide by Zero Error' type 'E'.
endif.
endcase.
when 'PRESET'.
var1 = 0.
var2 = 0.
res = 0.
op = 0.
when 'PBACK'.
res = res / 10.
when 'ONOFF'.
on = 1.
if on = 1.
loop at screen.
if screen-group1 = 'G1'.
screen-active = 1.
modify screen.
endif.
endloop.
else.
loop at screen.
if screen-group1 = 'G1'.
screen-active = 0.
modify screen.
endif.
endloop.
endif.
when 'BACK' or 'CANCEL' or 'EXIT'.
leave program.
endcase.
endmodule. " USER_COMMAND_0100 INPUT

This code can be enhanced with facilities like handling decimals and scientific functions.

No comments:

Post a Comment