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: 

Generate pushbuttons with loops

0 Kudos
748

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 🙂

8 REPLIES 8

former_member763929
Participant
0 Kudos
571

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.

FredericGirod
Active Contributor
571

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)

matt
Active Contributor
571
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.

FredericGirod
Active Contributor
571

matthew.billingham you think ABAP kernel will accept this loop ?

0 Kudos
571

How simple it is... unbelievable.

Thank you, guys 🙂

RaymondGiuseppi
Active Contributor
571

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.

matt
Active Contributor
0 Kudos
571

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.

jmodaal
Active Contributor
571

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.