assign an event to a button

hi everyone

i want to assign a click event to a button after open a dialog on natural 5.1.1
how could i do that?

i used this code to create button

DEFINE DATA
LOCAL
1 #NEW2 HANDLE OF pushbutton
END-DEFINE

PROCESS GUI ACTION ADD WITH
PARAMETERS
HANDLE-VARIABLE = #NEW2
TYPE = PUSHBUTTON
NAME = #I
STRING = ‘(Untitled)’
RECTANGLE-X = #J
RECTANGLE-Y = 311
RECTANGLE-W = 78
RECTANGLE-H = 59
PARENT = #DLG$WINDOW
END-PARAMETERS GIVING *ERROR
.

Version 5 is too old for me to remember much about it.

In Natural 8.3.8 I created a dialog and dropped a push button onto it from the Dialog Editor Insert toolbar. Then I selected Event Handlers from the context menu (right-click on the button). There I entered code to write a simple message (i.e. my event). Natural generated the following source code. Look for “write ‘pressed the button’”.

/** DIALOG SOURCE 22D
/*[ DEFINE DIALOG INFO
**D* NATURAL Dialog Description 8.3.8.0 / 2018-01-28 20:17
/** EMPTY DIALOG COMMENT
/*] END-DIALOG-INFO
DEFINE DATA
   /*[ DEFINE DIALOG PARAMETERS
   PARAMETER
      01 #DLG$PARENT HANDLE OF GUI BY VALUE
      /** EMPTY USER CODE SECTION
      /*[ DEFINE USING
      /*] END-USING
   /*] END-DIALOG-PARAMETERS
   /*[ DEFINE LOCAL DATA
   LOCAL
      /*[ DEFINE HANDLES
         01 #DLG$WINDOW HANDLE OF WINDOW
         01 #PB-1 HANDLE OF PUSHBUTTON
      /*] END-HANDLES
      /** EMPTY USER CODE SECTION
      /*[ DEFINE USING
         LOCAL USING NGULKEY1
         LOCAL USING NGULFCT1
      /*] END-USING
   /*] END-LOCAL-DATA
END-DEFINE
DEFINE SUBROUTINE #DLG$SUBR$CREATE$WINDOW /** DEFINE CONTROL #DLG$WINDOW
   PROCESS GUI ACTION ADD WITH
   PARAMETERS
      HANDLE-VARIABLE = #DLG$WINDOW
      TYPE = WINDOW
      STRING = '(Untitled)'
      STYLE = 'PZ'
      BITMAP-FILE-NAME = 'default.ico'
      PARENT = #DLG$PARENT
      CLIENT-DATA = *DIALOG-ID
      HORIZ-SCROLLABLE = FALSE
      VERT-SCROLLABLE = FALSE
      EVENT-QUEUEING = FALSE
      RECTANGLE-W = 865
      RECTANGLE-H = 420
      SCROLLRANGE-X = 208
      SCROLLRANGE-Y = 117
      VERSION = '08030800'
      SUPPRESS-ENTER-EVENT = SUPPRESSED
      SUPPRESS-LEAVE-EVENT = SUPPRESSED
      SUPPRESS-SIZE-EVENT = SUPPRESSED
   END-PARAMETERS GIVING *ERROR
END-SUBROUTINE /** END-CONTROL
DEFINE SUBROUTINE #DLG$SUBR$CREATE$CONTROLS /** DEFINE DIALOG ELEMENTS
   /*[ DEFINE CONTROL #PB-1
      PROCESS GUI ACTION ADD WITH
      PARAMETERS
         HANDLE-VARIABLE = #PB-1
         TYPE = PUSHBUTTON
         STRING = 'myPushbutton'
         RECTANGLE-X = 82
         RECTANGLE-Y = 55
         RECTANGLE-W = 126
         RECTANGLE-H = 62
         PARENT = #DLG$WINDOW
      END-PARAMETERS GIVING *ERROR
   /*] END-CONTROL
END-SUBROUTINE /** END-DIALOG-ELEMENTS
DEFINE SUBROUTINE #DLG$HANDLER$DEFAULT /** DEFINE EVENT DEFAULT
   OPTIONS 2 DEFAULT
   ;/** EMPTY USER CODE SECTION
   OPTIONS 3
END-SUBROUTINE /** END-EVENT
/*[ DEFINE EVENT BEFORE-ANY
   OPTIONS 2 BEFORE-ANY
   ;/** EMPTY USER CODE SECTION
   OPTIONS 3
/*] END-EVENT
DECIDE ON FIRST *CONTROL /** DEFINE ALL EVENTS
   /*[ DEFINE EVENTS FOR DIALOG
   VALUE NULL-HANDLE
      DECIDE ON FIRST *EVENT
         /*[ DEFINE EVENT OPEN
         VALUE 'OPEN'
            OPTIONS 2 BEFORE-OPEN
            ;/** EMPTY USER CODE SECTION
            OPTIONS 3
            PERFORM #DLG$SUBR$CREATE$WINDOW
         /*] END-EVENT
         /*[ DEFINE EVENT AFTER-OPEN
         VALUE 'AFTER-OPEN'
            PERFORM #DLG$SUBR$CREATE$CONTROLS
            PROCESS GUI ACTION AFTER-CREATION WITH #DLG$WINDOW GIVING *ERROR
            PROCESS GUI ACTION RESET-ATTRIBUTES GIVING *ERROR
            OPTIONS 2 AFTER-OPEN
            ;/** EMPTY USER CODE SECTION
            OPTIONS 3
         /*] END-EVENT
         /*[ DEFINE EVENT CLOSE
         VALUE 'CLOSE'
            OPTIONS 2 CLOSE
            ;/** EMPTY USER CODE SECTION
            OPTIONS 3
            PROCESS GUI ACTION DELETE-WINDOW WITH #DLG$WINDOW GIVING *ERROR
            ESCAPE ROUTINE IMMEDIATE
         /*] END-EVENT
      NONE
         PERFORM #DLG$HANDLER$DEFAULT
      END-DECIDE
   /*] END-EVENTS
   /*[ DEFINE EVENTS FOR #PB-1
   VALUE #PB-1
      DECIDE ON FIRST *EVENT
         /*[ DEFINE EVENT CLICK
         VALUE 'CLICK'
            OPTIONS 2 CLICK
            write 'pressed the button'
            OPTIONS 3
         /*] END-EVENT
      NONE
         PERFORM #DLG$HANDLER$DEFAULT
      END-DECIDE
   /*] END-EVENTS
NONE
   PERFORM #DLG$HANDLER$DEFAULT
END-DECIDE /** END-ALL-EVENTS
/*[ DEFINE EVENT AFTER-ANY
   OPTIONS 2 AFTER-ANY
   ;/** EMPTY USER CODE SECTION
   OPTIONS 3
/*] END-EVENT
END
/** END-DIALOG-SOURCE

If you are going for a dynamic approach (not using the build-in facilities of the dialog editor), you can catch the event in the BEFORE-ANY, AFTER-ANY or the IDLE events.

The *EVENT = ‘CLICK’ will indicate a click on any given GUI element that is clicked, so you need to identify which element was clicked.
This can be done by checking if *CONTROL = the button you want to handle:

[in the AFTER-ANY event]
if *event = ‘CLICK’ and *control = #mybutton
print ‘My button was clicked’ newpage
end-if

… an even more dynamic approach would be to use CLIENT-KEY/-VALUE to determine which unique button was clicked:

[in PARAMETERS when adding the button]
CLIENT-KEY = ‘id’
CLIENT-VALUE = ‘myAction’

[define as local variable]
1 #focus handle of gui

[in the AFTER-ANY event]
if *event = ‘CLICK’
#focus := *control
#focus.client-key := ‘id’
if #focus.client-value = ‘myAction’
print ‘The “myAction” button was clicked’ newpage
end-if
end-if