Close Method, AutoClose Property , Close Event, Destroy Event

Purpose

The Close method closes a Form window. The Close event occurs when a window is about to close. The AutoClose property prevents automatic closure of a Form.

Syntax

Form.Close

Form.AutoClose [= value ]

Sub Form_Close( [index%,] Cancel? )

Sub Form_Destroy( [index%] )

value:Bool exp
index%:iexp, form number
Cancel?:boolean ByRef

Description

When AutoClose = 0 the form is not automatically closed when Alt-F4 is pressed, or when the close button in the caption is clicked. Instead, the program must handle the Form_Close(Cancel?) sub event to close the window by setting Cancel? = False. The ByRef parameter Cancel? is True by default, so that without changing it, the window isn't closed.

To explicitly close a window the Close method is available. This method will not result in invoking the Close event sub. A window can also be closed by setting its object variable to Nothing (Set Win_1 = Nothing).

After closing a window/form/dialog the Destroy event is invoked. This is the place to release resources and finalize the Form. The Destroy event is generated when DestroyWindow is called. Windows doesn't send WM_CLOSE and WM_DESTROY messages when the user logs off. The QueryEndSession event is the time to do the final things.

Example

OpenW 1

Me.AutoClose 0

Do

Sleep

Until Me Is Nothing

 

Sub Win_1_Close(Cancel?)

If MsgBox("Close Form?", MB_YESNO) = IDYES Then Cancel? False

EndSub

 

Sub Win_1_Destroy

' release resources

EndSub

Remarks

For all forms AutoClose = True by default, except for Dialogs, where Dlg_n.AutoClose = False.

In addition, unlike CloseW, if Form.Close is used for a form which does not exist or has been set to Nothing, an error is returned.

See Also

Form, QueryEndSession

{Created by Sjouke Hamstra; Last updated: 12/05/14 by James Gaite}