ObjectDCL Banner

OnCancelClose Event

Description

This event is fired to query if the dialog should be closed. Return a 1 to indicate that your program wishes to cancel the close action.

Parameters

bUserPressedEsc
Boolean identifying whether the dialog is being close because the user pressed Escape.

AutoLISP Syntax

Each dialog that throws this event has its handler that takes this generic form:

CopyCode imageCopy Code
(defun c:FormName_OnCancelClose (bUserPressedEsc [as Boolean] /)
     
)

Applies For

Modal, Modeless.

Example

Modal looping dialogModalLooping dialog
This example shows how to control looping a modal dialog in a safe way. You can find the complete example in the ModalLooping.zip archive.

NOTE: Never call Odcl_Form_Show from a dialog event handler or you will cause the stack to overflow. Always unroll a Odcl_Form_Show before taking the next action.

This example demonstrate how to achieve a loop without getting into a stack overflow.

The loop variable controls the continuation. Once set to Nil, the looping stops.

The action variable identifies which button the user pressed.

There are five possible ways the dialog can be closed and they all are covered in this example:

  • The user presses OK
  • The user presses Action
  • The user presses Cancel
  • The user hits Escape
  • The user clicks the X box in the upper right of the dialog

CopyCode imageCopy Code
;;================================================;;
;; ObjectDCL Best Practice Advice!                ;;
;;================================================;;
;;  Note: Never call Odcl_Form_Show from a        ;;
;;        dialog event handler or you will cause  ;;
;;        the stack to overflow. Always unroll    ;;
;;        a Odcl_Form_Show before taking the      ;;
;;        next action.                            ;;
;;================================================;;

(defun c:ModalLooping ()
  (odcl_loadproject "ModalLooping.odc" T)
  (setq loop T)
  (while loop
    (setq action Nil)
    (Odcl_Form_Show ModalLooping_ModalLoopingForm)
    (if
      (eq action "DoAction")
      (princ "...Executing action A...n")
    )
    (if
      (eq action "DoOk")
      (princ "...Executing action B...n")
    )
    (if
      (Not action)
      (or
        (princ "The user pressed Cancel, Escape or the X button...n")
        (setq loop Nil)
      )
    )
  )
  (princ "The user is bored and now leaves back home...n")
)

;;================================================;;
;; OnClicked ActionButton event handler           ;;
;;================================================;;

(defun c:ModalLoopingForm_ActionButton_OnClicked ()
  (princ "The user want to do action A...n")
  (setq action "DoAction")
  (Odcl_Form_Close ModalLooping_ModalLoopingForm)
)

;;================================================;;
;; OnClicked OkButton event handler               ;;
;;================================================;;

(defun c:ModalLoopingForm_OkButton_OnClicked ()
  (princ "The user want to do action B...n")
  (setq action "DoOk")
  (Odcl_Form_Close ModalLooping_ModalLoopingForm)
)

;;================================================;;
;; OnClicked CancelButton event handler           ;;
;;================================================;;

(defun c:ModalLoopingForm_CancelButton_OnClicked ()
  (princ "The user has cancelled...n")
  (setq loop Nil)
  (Odcl_Form_Close ModalLooping_ModalLoopingForm)
)

;;================================================;;
;; OnClose Dialog event handler                   ;;
;;================================================;;

(defun c:ModalLoopingForm_OnClose (nUpperLeftX nUpperLeftY /)
  (princ "Dialog closed...n")
)

;;================================================;;
;; OnCancelClose Dialog event handler             ;;
;;================================================;;
;; Returns                                        ;;
;;    T to prevent closing the dialog             ;;
;;    nil to allow closing the dialog             ;;
;;================================================;;

(defun c:ModalLoopingForm_OnCancelClose (bUserPressedEsc /)
  (princ "Dialog about to be closed...n")
  (if
    (eq bUserPressedEsc 1)
    (or
      (princ "The user pressed escape...n")
      (setq loop Nil)
    )
  )
  Nil
)
)

See Also

OnClose, OnInitialize