Liberty BASIC Help Online

Trapping the Close Event
 
It is important for Liberty BASIC program windows to trap the close event.  Then when a user tries to close a window, program flow is directed to an event handler that the program specifies.  At that place the program can ask for verification that the window should be closed, and/or perform some sort of cleanup (close files, write ini data, set a flag that the window is open or closed, etc.).  There may be a menu item or button that a user can click to close the window, but the user might also click the X closing button or the system close button, and that is the event trapped by the trapclose statement.
 
The trapclose command works with all window types.
 
Here is the format for trapclose:
 
  print #myWindow, "trapclose [branchLabel]"
  print #myWindow, "trapclose subLabel"
  #myWindow "trapclose [branchLabel"
 
This will tell Liberty BASIC to use the code at [branchLabel] as an event handler for the window with the handle #myWindow, continuing execution of the program there if the user tries to close the window (see buttons1.bas example below). If the subroutine, subLabel is designated as the event handler, rather than a branch label, the named subroutine is executed when the close event is triggered. The handle of the window is passed into the subroutine by Liberty BASIC.
 
Usage with branch label handler:
The trapclose code in buttons1.bas looks like this:
 
    open "This is a turtle graphics window!" for graphics_nsb as #1
    print #1, "trapclose [quit]"
 
    ' stop and wait for buttons to be pressed
    wait
 
And then the code that is executed when the window is closed looks like this:
 
[quit]
    confirm "Do you want to quit Buttons?"; quit$
    if quit$ = "no" then wait
    close #1
    end
 
 
Usage with subroutine handler:
The trapclose code in buttons1.bas would look like this if a subroutine was used as the handler:
 
    open "This is a turtle graphics window!" for graphics_nsb as #1
    print #1, "trapclose Quit"
 
    ' stop and wait for buttons to be pressed
    wait
 
And then the code that is executed when the window is closed looks like this:
 
sub Quit handle$
    confirm "Do you want to quit Buttons?"; quit$
    if quit$ = "no" then wait
    close #handle$
    end
    end sub
 
 
 


Copyright (C) 2003 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/