FILE: Use Topclose.exe to Close Child Forms Of Top-Level FormsLast reviewed: October 24, 1997Article ID: Q175625 |
The information in this article applies to:
SUMMARYOne of the sample project files for this article provides an example of a way to close a Top-Level form and have the user prompted to close each child form. Another sample project file shows a way to try to do this that does not work. Still another sample project file shows a way to accomplish the task that almost works or will work in limited circumstances. The following file is available for download from the Microsoft Software Library:
~ TopClose.exe (size: 72854 bytes)For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q119591 TITLE : How to Obtain Microsoft Support Files from Online Services MORE INFORMATIONIn Visual FoxPro 5.0, forms can be set to show as Top-Level forms. Other child forms can be opened in the Top-Level form. This article will discuss a way to close the Top-Level form and have the user prompted for saving changes to any child forms contained in the Top-Level form. The solution presented will work the best in Visual FoxPro 5.0a and only partially in Visual FoxPro 5.0 due to changes in how the QueryUnload method is called. Note that the solution presented here is not necessarily the only way to handle this situation. More sophisticated schemes can be developed. In fact, this article does not consider the case of Windows being exited with the application running. Also note that the scenario presented here is fairly complex. Microsoft Product Support Engineers will not know all the issues presented in this article without spending some time working with the sample files. Please remember this if asking questions on the enclosed files.
Additional InformationThe following files are included in this article and are used in the discussion that follows: The TopClose.exe should extract into three folders, TopClose\Almost, TopClose\Bad and TopClose\Good. Contents of TopClose\Almost:
Contents of TopClose\Bad:
ARTICLE-ID: Q175682 TITLE : PRB: VFP Crashes When Working With Top-Level & Child FormsDiscussion Of Sample Files' Behavior Normally the QueryUnload method can be used in a form to determine if a form is ready to be closed. The Online Help for Visual FoxPro states that it "...occurs when CLEAR WINDOWS, RELEASE WINDOWS, or QUIT is executed in code, when the user double-clicks the Control-menu box, or when the user chooses Close from the Control-menu on a form." If the form is contained in a Top-Level form and the Top-Level form is closed as described above, the QueryUnload method of any child forms does not behave as expected. In Visual FoxPro 5.0, the QueryUnload method of child forms is executed before the QueryUnload of the Top-Level parent form. This is reversed in Visual FoxPro 5.0a. Here the QueryUnload of the Top-Level parent form is executed before the child forms' QueryUnload. Therefore the main difference between how the two versions behave is that Visual FoxPro 5.0a will ask if the Top-Level form is ready to be closed while Visual FoxPro 5.0 will not. This is because the code that asks this question is in the QueryUnload method of the Top-Level form which is not executed in Visual FoxPro 5.0. In either case, if the child forms are not closed or NODEFAULT is not encountered in the QueryUnload of the Top-Level form, the user is essentially stuck in an endless loop getting asked if it is OK to close each child form. This will only stop if there is only one child form left. Run the Bad.app to see this illustrated. There are instructions on the main Top-Level form for what to do to illustrate the problem. What seems to be the best way to handle this situation is to put code in the QueryUnload methods of both the Top-Level form and the child forms. If the QueryUnload method in the Almost.scx form is reviewed, it would appear that this should be all that is necessary to close the child forms. Unfortunately, due to what appears to be a bug in Visual FoxPro, this technique cannot be used in all circumstances. Only if the Top-Level form is the main and only interface for a distributed executable file does this technique seem not to cause problems. Whether using the technique in the Good.app or the Almost.app(not really recommended), additional code can be placed in the QueryUnload methods of the child forms in case they are closed by their control boxes. Below is the QueryUnload method code from the Top-Level form and one of the child forms in the Good.app example. Also review the code in the actual form's methods and the other files in the project.
*** QueryUnload of Good Top-Level form LOCAL lAllClosed, lFormOpen lAllClosed=.T. lFormOpen=.F. LOCAL xx xx=_Screen.FormCount * The CloseBox or Close command in the application menu was chosen IF This.ReleaseType=1 IF MESSAGEBOX(; "Are you ready to close this top-level form?",36,; "QueryUnload of "+THIS.Caption) = 7 NODEFAULT lAllClosed=.F. RETURN lAllClosed ENDIF ENDIF * The Quit menu option was chosen IF This.ReleaseType=0 AND xx>1 IF MESSAGEBOX(; "Are you ready to close this top-level form?",36,; "QueryUnload of "+THIS.Caption) = 7 NODEFAULT lAllClosed=.F. RETURN lAllClosed ENDIF ENDIF * Attempt to close all active forms via each form's QueryUnload event DO WHILE TYPE('_SCREEN.ACTIVEFORM') = "O" AND ; _SCREEN.ACTIVEFORM.NAME <> "_" IF NOT _SCREEN.ACTIVEFORM.QUERYUNLOAD() lAllClosed = .F. NODEFAULT EXIT ENDIF ENDDO RETURN lAllClosed *** End of QueryUnload of Good Top-Level form ********************** *** QueryUnload of a Child form - either the GoodRed or GoodGreen.scx LOCAL x,xx,ans,cTopLevelCaption * Getting the caption of the Top-Level form to use in dialog boxes xx = _SCREEN.FormCount FOR x = 1 TO xx IF _SCREEN.FORMS[x].NAME = "_" cTopLevelCaption = _SCREEN.FORMS[x].Caption EXIT ENDIF ENDFOR ****** * Optionally save changes before closing the form ans = MESSAGEBOX(; "Do you want to save changes to the "+CHR(13)+; THIS.Caption+"?",35,cTopLevelCaption) DO CASE CASE ans = 6 && Yes WAIT WINDOW "Changes saved" TIMEOUT 1 CASE ans = 7 && No WAIT WINDOW "Changes NOT saved" TIMEOUT 1 CASE ans = 2 && Cancel * Abort closing this form NODEFAULT * Return to calling program RETURN .F. ENDCASE THIS.RELEASE && if QueryUnload called as a methodRun the Good.app in Visual FoxPro 5.0a to see the code above in action. In the Good.app example, Visual FoxPro 5.0 and 5.0a both behave the same way if the Quit menu option is chosen. Quit calls the QueryUnload method of the Top-Level form and if that returns true(.T.) then the Top-Level form is released and CLEAR EVENTS is issued. Note that the Destroy method of the Top-Level form issues a CLEAR EVENTS. There is a difference between Visual FoxPro 5.0 and 5.0a if the CloseBox is used to close the Top-Level form when the child forms are open. In Visual FoxPro 5.0, the GoodRed and GoodGreen forms are closed since they don't have anything in their QueryUnload methods to prevent that. The GoodBlue form has code in it's QueryUnload method that can be used to keep the form from closing. But if two instances of the GoodBlue form are running, answering "No" when prompted to close will put Visual FoxPro into the same endless loop illustrated in Bad.app. This happens with both the Almost.app and Good.app in Visual FoxPro 5.0. This does not happen in Visual FoxPro 5.0a with either application.
REFERENCESVisual FoxPro Help; search on ShowWindow property and QueryUnload method. (c) Microsoft Corporation 1997, All Rights Reserved. Contributions by John W. Spilker, LEAD Associates, Inc. Keywords : FxprgClassoop vfoxwin kbcode Version : WINDOWS:5.0,5.0a Platform : WINDOWS Issue type : kbfile Solution Type : kbsample |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |