Test for a click on the Close (X) button on a popup

by graybin at 2012-11-23 09:30:44

I can’t seem to find out how to test that. I can use Add_click if someone clicks on a button I created.
I can event test if someone hits “enter” or “escape”, but how do I know if someone clicks on the x to close the popup, and how do I run some code before it actually closes?
by DonJ at 2012-11-23 12:14:40
You don’t mention what kind of “popup,” but generally speaking you can’t test for a click on the “X” on a dialog box. Instead, you have to be notified of the form’s closing event. See http://msdn.microsoft.com/en-us/library … osing.aspx. That event gets triggered no matter how the form is closed; you shouldn’t be testing for keypresses like Escape or Enter. Enter should trigger your form’s default button, and you respond to its Click event; Escape closes the form (like clicking the X button) and you respond to Closing.
by graybin at 2012-11-26 08:21:30
I found the way to do it:

$objForm = New-Object System.Windows.Forms.Form
$objForm.Add_FormClosed({ <code to run when form is closed goes here> })
by DonJ at 2012-11-26 08:26:00
There is a subtle difference between the Closing and Closed events; just review the MSDN docs quickly and make sure you’re attaching the one that’ll work for whatever you’re doing in there.
by graybin at 2012-11-26 10:20:51
Thanks, I see the difference. For my purposes, they are both fine.