WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

FAQs - Frequently Asked Questions

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

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:
:CANCEL 
in 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	(s)	Cancel line (ie, line in script that caused Cancel)
wberrorhandleroffset	(i)	offset into script of Cancel line, in bytes
wberrorhandlerassignment(s)	variable being assigned on Cancel line, or "" if none
wberrorhandlerfile	(s)	name of the currently-executing script
wberrortextstring	(s)	description of the WIL error
wberroradditionalinfo	(s)	additional error information, if any
wberrorinsegment 	(s)	name of the currently-executing UDF, or a blank string ("") if not in a UDF.
wberrorarray		(a)	WIL array with the following elements:
				wberrorarray[0] = LastError()
				wberrorarray[1] = wberrorhandlerline
				wberrorarray[2] = wberrorhandleroffset
				wberrorarray[3] = wberrorhandlerassignment
				wberrorarray[4] = wberrorhandlerfile
				wberrorarray[5] = wberrortextstring
				wberrorarray[6] = wberroradditionalinfo
				wberrorarray[7] = wberrorinsegment

Note: 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%
exit

Another 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%
EXIT 

TIP 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:   W12994
Filename:   How to Prevent Cancel Button from Quitting App.txt
File Created: 2004:09:27:12:41:54
Last Updated: 2004:09:27:12:41:54