views:

201

answers:

1

I have a VSPackage with a dockable tool window containing form data. If there are unsaved changes in this form, I would like to cancel a close to either the tool window and the visual studio IDE if the user clicks cancel on saving changes before closing. I can perform the save test on close, but I don't see any event handler methods or other options to actually cancel the close.

Here is some blurb from the package:

    private DTE2 _applicationObject = null;
    ///--------------------------------------------------------------------------------
    /// <summary>This property gets the visual studio IDE application object.</summary>
    ///--------------------------------------------------------------------------------
    public DTE2 ApplicationObject
    {
        get
        {
            if (_applicationObject == null)
            {
                // Get an instance of the currently running Visual Studio IDE
                DTE dte = (DTE)GetService(typeof(DTE));
                _applicationObject = dte as DTE2;
            }
            return _applicationObject;
        }
    }
    ///--------------------------------------------------------------------------------
    /// <summary>
    /// Initialization of the package; this method is called right after the package is sited, so this is the place
    /// where you can put all the initilaization code that rely on services provided by VisualStudio.
    /// </summary>
    ///--------------------------------------------------------------------------------
    protected override void Initialize()
    {
        Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
        base.Initialize();

        // add the event handlers
        if (ApplicationObject != null)
        {
            // wire up window events
            PackageWindowEvents = (WindowEvents)ApplicationObject.Events.get_WindowEvents(null);
            PackageWindowEvents.WindowClosing += new _dispWindowEvents_WindowClosingEventHandler(PackageWindowEvents_WindowClosing);

            // wire up solution events
            PackageSolutionEvents = ApplicationObject.Events.SolutionEvents;
            PackageSolutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(CheckOpenCurrentSolution);

            // wire up DTE events
            PackageDTEEvents = ApplicationObject.Events.DTEEvents;
            PackageDTEEvents.OnBeginShutdown += new _dispDTEEvents_OnBeginShutdownEventHandler(HandleVisualStudioShutdown);
        }
    }

    void PackageWindowEvents_WindowClosing(Window window)
    {
        // handle save/cancel scenarios
    }

And some blurb from the ToolWindowPane that implements IVsWindowFrameNotify3:

    protected override void OnClose()
    {
        base.OnClose();
    }
    public int OnClose(ref uint pgrfSaveOptions)
    {
        return (int)__FRAMECLOSE.FRAMECLOSE_PromptSave;
    }

The OnClose and window closing methods fire when expected, but I'm not finding a way to cancel the close. What am I missing? Are there different events required to cancel a close?

A: 

Hmm, seems this isn't the best forum for extensibility (VSPackage) issues, no views.

I got some responses from the msdn forum:

http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/66e6d99e-dc39-4984-b8af-9f528f9f9042

The basic answer is that there is no interface to cancel a close with a tool window, since a tool window doesn't really close and is not designed to manage data. Canceling a close can be done with a document window, but a document window is confined to the MDI area.

The solution for my business case may not fit either tool windows or document windows. I have a high level tree view control that manages xml files that are associated with solutions, and the child form controls manage nodes in the tree view. The best fit for this situation may be with domain specific language tools:

http://msdn.microsoft.com/en-us/library/bb126235.aspx

Dave Clemmer