Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have this problem.. i want to know if there is way that the program will detect if the user clicks the close (X button on the upper right of the window) ? say on the sub form

then the program will close the main form and all the other sub forms....


pls.. help..


Thanks...
Posted

Easy - add a handler to the sub form FormClosed event when you construct it (and before you use the Show function).
In the handler, close the current form.


[edit]Forgot the Form part of FormClosed... - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 3-Jul-11 3:28am    
Did you mean "FormClosing" as you can cancel, check up the reason, etc.? "FormClosed" can be too late.
Please see my solution.
--SA
OriginalGriff 3-Jul-11 3:34am    
Normally, I would handle the Closed event - leave the reasons and any cancellation to the sub form, as it knows what it is doing when the user decided to close it.
Sergey Alexandrovich Kryukov 3-Jul-11 3:44am    
Anyway, I mentioned both events because either of them or both might be really needed.
--SA
You need to override the method System.Windows.Forms.OnFormClosing. You may cancel closing the form my assigning true the the property Cancel of event arguments parameter. You can get the same effect by handling the event System.Windows.Forms.FormClosing. You can also check the closing reason passed in the same argument and act accordingly.

The event System.Windows.Forms.FormClosed will "detect" it, too, but this is too late to change or cancel — the form will be closed anyway.

—SA
 
Share this answer
 
I would use the my.application.openforms

on the form close events call a rountine that uses the my.application.openforms

Openforms[^]

you will need to enumerate that form collection but remember to the active form last.

this way you can also perform any save / delete functions that you want before the form is closed.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 3-Jul-11 12:31pm    
You can use it of course, but this is not an event; you're not getting a notification. The question was how to detect. If you detect closing window, you already have what you need.
--SA
Simon_Whale 3-Jul-11 14:22pm    
Good point that was omitted the form closed and closing events. But I did assume that with the first and second answers that the OP would of realised to use those events to start the process that he is after
Sergey Alexandrovich Kryukov 3-Jul-11 16:38pm    
Understood.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900