Giving the focus to a form element

How to force the focus to move or stay in a specific form element using program code.

Use the NEXT FIELD instruction to force the focus to a specific field or screen record (list). The NEXT FIELD instruction expects a form field name.

In a DIALOG block, when the specified field is the first column identifier of a sub-dialog driven by a DISPLAY ARRAY block, the read-only list gets the focus. If the field name is not known at compile time, you can alternatively use the ui.Dialog.nextfield() method.
DIALOG ATTRIBUTES(UNBUFFERED)
   INPUT BY NAME p_cust ATTRIBUTES(NAME="cust")
     ...
   END DISPLAY
   DISPLAY ARRAY p_orders TO orders.*
     ...
   END DISPLAY
   ON ACTION go_to_header 
      NEXT FIELD cust_num 
   ON ACTION go_to_detail 
      NEXT FIELD order_lineno 
   ...
END DIALOG
When a BUTTON exists in the form layout, it can get the focus if the DIALOG block defines a COMMAND clause as action handler. Currently there is no way to give the focus to a BUTTON by program.
DIALOG ATTRIBUTES(UNBUFFERED)
   ...
   COMMAND "print" 
      CALL print_order()
   ...
END DIALOG
In rare cases (especially when using folder tabs), it may be required to show a part of the form that is not controlled by the dialog, when there is no active field or button that can get the focus in that part of the form, and when the above techniques cannot work. In this case, in order to show temporarily a given part of the form that cannot get the focus, you use the ui.Form.ensureFieldVisible or ui.Form.ensureElementVisible methods.
DEFINE form ui.Form
...
DIALOG ATTRIBUTES(UNBUFFERED)
   ...
   BEFORE DIALOG
      LET form = DIALOG.getForm()
   ...
   ON ACTION show_image1 
      CALL form.ensureElementVisible("image1")
   ...
END DIALOG

When using the FOCUSONFIELD attribute of DISPLAY ARRAY, you can set the focus to a specific cell by using the NEXT FIELD instruction instruction or the ui.Dialog.nextField() method, in conjunction with the ui.Dialog.setCurrentRow() method. For more details, see Field-level focus in DISPLAY ARRAY.