13
votes

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?

3
If you click "X", but "Save Changes" won't appear, does the problem still occurs?Eran Betzalel
Is there a reason you're not doing the validation and such in the "Save Changes" dialog methods? Commonly, one would have the window-close events check for changes and initiate a "Save Changes" prompt. The "Save Changes" method would then return True if validation was good and the save was successful, or False if validation failed... Then that value would get sent back up to the window-close event handler, True to go ahead and close the window or False to reject it and do nothing.ewall

3 Answers

1
votes

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
}
0
votes

I would suggest, check on the lines of handling MDI Child window events!!

The editor tab you are referring is basically an instance of MDI Child Window.

Hope this helps!

0
votes

If you're willing to use some Windows API code you might be able to set up a hook using the SetWindowsHookEx function to intercept WM_CLOSE, WM_QUIT and WM_DESTROY.