A possible solution (I use in C #) is to store each instance of the open form in the var list. For example, you might have a global list called openForms; when each form is created, the form itself can add its own link to openForms and delete it when closing.
When a user tries to close your application, you can verify that the number of lists is greater than zero, and if the user really wants to close, you gracefully close each instance of the form contained in openForms before closing the application.
Marco source share