How to Prevent Cancel Button from Quitting Application
Keywords: recovering from cancel terminate cancel button oncancel
Question:
How do I get WinBatch to not terminate when a Cancel button is pushed?Answer:
Generally the CANCEL will cause the script to terminate. When a user presses the CANCEL button, we search for a label::CANCELin the WinBatch script. If we do not find it, the script is cancelled. If we find it the script continues.The function IntControl( 72,…) lets you specify what should happen when the next Cancel event occurs in the script. This allows the program developer to perform cancel processing, if a user presses Cancel, in any dialog. When processing goes to :CANCEL, the following WIL variables are automatically set:
Variable Type Meaning ------------------------------------------------ wberrorhandlerline string Cancel line (i.e., line in script that caused Cancel) wberrorhandleroffset integer offset into script of Cancel line, in bytes wberrorhandlerassignment string variable being assigned on Cancel line, or "" if noneNote: The Windows operating system offers many alternatives, for canceling a dialog: pressing the Cancel button, typing Esc, Alt-F4, and some others.
The following is sample show how to recover from the user hitting cancel:
;TITLE: ;DATE: ;AUTHOR: ;VERSION: ;UPDATED: ;PURPOSE: IntControl(72,2,0,0,0) abc=Pause("Pause","one") Message("ABC is",abc) def=Pause("Pause","two") Message("def is",def) Exit :CANCEL %wberrorhandlerassignment% = 9999 IntControl(72,2,0,0,0) return
The following is another sample, not using the IntControl (72..):
;; TOP OF FILE ONCANCEL="EXIT" Dialog1(xxxxx etc ) ONCANCEL="GOTO DIALOG" Dialog2(xxxxx etc. ) ONCANCEL="GOTO DIALOG2" Dialog(xxxx etc. ) :CANCEL %ONCANCEL% EXIT
Or another example:
<-------snip---------> ; Simple example a=AskYesNo("testing","Press the cancel button") :can1 Message("Testing","Value of A is %a%") exit :cancel a=-1 goto can1 <-------------snip------------->Advanced Example:
<---------snip--------------> oncancel="exit" xxx=AskYesNo("Testing","If you press cancel now the script will exit") a=-1 oncancel="goto can1" a=AskYesNo("Testing","Press Cancel") :can1 Message("testing","Value of A is %a%") a=-1 oncancel="goto can2" a=AskYesNo("Testing","Press Cancel") :can2 Message("testing","Value of A is %a%") oncancel="exit" xxx=AskYesNo("Testing","If you press cancel now the script will exit") exit :cancel %oncancel% exitAnother Example:
ONCANCEL="GOTO REDRAW" :REDRAW types="All Files|*.*|WIL Files|*.wbt;*.mnu|Text Files|*.txt|" fn1=AskFileName("SelectFile", "C:\WinBatch", types, "Sol.wbt", 1) Message("File selected was", fn1) ;EEEP ADD EXIT :CANCEL %ONCANCEL% EXITTIP Regarding Redefining Cancel in Dialog Editor:
The problem with :CANCEL subsections for me is often one of returning to the same level of structure at which the dialog resides. If the dialog with the cancel button is embedded in a different structure level than the :CANCEL subsection, then using a GOTO command to return to the point in the script immediately after the dialog is often very complicated if not impossible.One solution that works for me is to place the dialog in a separate wbt file that can be called from the main script.
In the subsidiary call file you can have a :cancel subsection where oncancel="Return", thus returning you to the main script at precisely the point you want to be.
Article ID: W12994Filename: How to Prevent Cancel Button from Quitting App.txt