I want to capture events that close editor window (tab) in Visual Studio 2008 IDE. When I use dte2.Application.Events.get_CommandEvents(null, 0).BeforeExecute I successfully captured such events:
If code in window is not acceptable, I stop the event (CancelDefault = true).
But if I click "X" button on the right hand side, "Save Changes"; dialog appears, tab with editor window close and I have no any captured events. In this case I can capture WindowClosing event, but can not cancel the event.
Is it poosible to handle "x" button click and stop event?
A tab or window closing in a browser can be detected by using the beforeunload event. This can be used to alert the user in case some data is unsaved on the page, or the user has mistakenly navigated away from the current page by closing the tab or the browser.
The Antiquated Candle Test: If you have a candle around, light it and hold it up to a closed door or window—not too far away and not too close. If the candle flame flickers and sputters, that's bad news; if the candle goes out, the news is far worse… You need a new door.
When we refresh the page (F5, or icon in browser), it will first trigger ONUNLOAD event. When we close the browser (X on right top icon),It will trigger ONUNLOAD event.
The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point. This event enables a web page to trigger a confirmation dialog asking the user if they really want to leave the page.
In C# it would be something like this: you add Closing event handler and then
void MyWindow_Closing(object sender, CancelEventArgs e)
{
if(something)
e.Cancel = true; //<- thats the magic part you want
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With