2021 Sep 29 11:20 AM
Hello 🙂
I'm new to ABAP and I have got a problem that I cannot solve on my own.
Is it possible to generate n buttons (or any other elements) using a loop? My first attempt was:
DO 6 TIMES.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN PUSHBUTTON 1(20) l_btn USER-COMMAND cmd.
SELECTION-SCREEN END OF LINE.
NEW-LINE.
ENDDO.
This one works fine for simple output:
DATA l_btn TYPE string VALUE 'Button'.
DO 6 TIMES.
WRITE: / l_btn && sy-index.
NEW-LINE.
ENDDO.
Any help is appreciated 🙂
2021 Sep 29 11:20 AM
Thank you for visiting SAP Community to get answers to your questions. Since this is your first question, I recommend that you familiarize yourself with Community Q&A , as the overview provides tips for preparing questions that draw responses from our members.
Should you wish, you can revise your question by selecting Actions, then Edit.
By adding a picture to your Profile you encourage readers to respond.
2021 Sep 29 11:28 AM
it is not possible to loop in selection screen
Selection screen generate a dynpro
WRITE statement, is only an output (it is not the same logicm, the same place)
2021 Sep 29 11:46 AM
DO 6 TIMES.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN PUSHBUTTON 1(20) l_btn USER-COMMAND cmd.
SELECTION-SCREEN END OF LINE.
NEW-LINE.
ENDDO.
is identical to
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN PUSHBUTTON 1(20) l_btn USER-COMMAND cmd.
SELECTION-SCREEN END OF LINE.
DO 6 TIMES.
NEW-LINE.
ENDDO.
Selection-screen is declarative, like DATA. It can't be looped over.
2021 Sep 29 11:52 AM
matthew.billingham you think ABAP kernel will accept this loop ?
2021 Sep 29 2:26 PM
2021 Sep 29 2:36 PM
You cannot use this kind of syntax, even when program is activated only one pushbutton will be displayed (the selection-screen dynpros are created and activated with the main program.)
You could
In the advanced solutions (aka funny solutions) you have to handle the click on button either with function code and GET CURSOR or handling event on_click.
NB: NEW-LINE is related to classic list (spool) not selection-screen, you have confused with SELECTION-SCREEN SKIP.
2021 Sep 29 8:22 PM
frdric.girod Do I think it will be accepted? I know it will. I tried it.
Both are syntactically correct, both run with the same result. But not what the OP wanted.
2021 Sep 30 1:03 PM
Hello,
as said it has to be somehow static to generate the selection screen. Depending on the amount of "nearly" identical resp. similar elements on the selection screen it could be an approach to use a macro. See example below. For 6 occurrences I would not do it, but it could make sense for a higher amount to shorten the code and avoid redundancy. But one might ask whether this is good code or terrible...
However, I just want to give the hint for the possibility to use a macro in the selection screen.
program Z_PB_TEST.
define SELECTION_PUSHBTN.
selection-screen begin of line.
selection-screen pushbutton 1(20) BUTTON&1 user-command CMD&1.
selection-screen end of line.
end-of-definition.
constants: PBTNCNT type i value 6.
SELECTION_PUSHBTN: 1,2,3,4,5,6.
initialization.
data: btnName type char20.
field-symbols: <btn> type any.
do PBTNCNT times.
btnName = | BUTTON { sy-index } |.
condense btnName no-gaps.
assign (btnName) to <btn>.
<btn> = | Button { sy-index } |.
enddo.
at selection-screen.
case sy-ucomm.
when 'CMD1'.
message i398(00) with 'BUTTON1 pressed'.
when 'CMD2'.
message i398(00) with 'BUTTON2 pressed'.
when 'CMD3'.
message i398(00) with 'BUTTON3 pressed'.
when 'CMD4'.
message i398(00) with 'BUTTON4 pressed'.
when 'CMD5'.
message i398(00) with 'BUTTON5 pressed'.
when 'CMD6'.
message i398(00) with 'BUTTON6 pressed'.
endcase.