Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture window close event

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:

  • File.Close
  • File.CloseAllButThis
  • File.Exit
  • Window.CloseDocumentWindow and others.

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?

like image 817
user167876 Avatar asked Sep 03 '09 13:09

user167876


People also ask

How do you take a close window 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.

How do you know when a window is closed?

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.

What is the difference between browser refresh and close?

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.

What is Beforeunload 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.


1 Answers

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
}
like image 77
m0s Avatar answered Oct 23 '22 11:10

m0s