open windows when terminating the application

When a window is closed, I can find out, if there are other windows, which are still open and I can close them. Then the application is really terminated. This I want to happen, when I click to the Close Button.
But when I close a window by program-code the close-event is firing as well and the program is closed down.

How can I find out, if it was the close button or a program-code, which fired the close-event?

Why not use Quit() when you want the program to close?

I want to use the close-button in each window to terminate the application.
and this fires the close event .

You can set a flag when you close the window in code and check that flag in the close event. Add a method to each window that you call to close it in code.

Sub CloseWindow
   ClosingFlag = True
   ClosingFlag = False
End Sub

In the window Close event

if ClosingFlag then return
// close all the other windows here

I had the idea myself, but it took me half hour to go through the whole program.
Find it a bit uncomfortable.