locked
Problem detecting actions pane closing event using CustomTaskPane.VisibleChanged event RRS feed

  • Question

  • I am using the VisibleChanged event of a Custom task pane in an Office 2010 Visual Studio Tools for Office Add-in. When the user closes the custom pane, I would like to prompt the user with a question (i.e., "Are you sure?").
     
    The problem I am having is that the event is also triggered when closing the document window.
     
    Is there a reliable way to distinguish between the action pane being closed by the 'x' button, vs. as an indirect result of the document closing? I have tried with a timer but it is unreliable especially if there are many document windows being closed simultaneously. To further confuse things, the Word app's DocumentChange event is firing infinitely when there is only one document left open...
     
    Thanks,
     wordyone

    (Note this was originally posted on the IT Pro forum but I have removed it because I meant to post on VSTO).

    Tuesday, November 8, 2011 5:38 PM

Answers

  • Hi wordyone

    I've never checked this, so... Is there any chance that DocumentBeforeClose might be firing before VisibleChanged? If it did, it could set a class level Boolean object that VisibleChanged could check before displaying the message.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by wordyone Friday, November 11, 2011 4:41 PM
    Tuesday, November 8, 2011 7:32 PM

All replies

  • Hi wordyone

    I've never checked this, so... Is there any chance that DocumentBeforeClose might be firing before VisibleChanged? If it did, it could set a class level Boolean object that VisibleChanged could check before displaying the message.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by wordyone Friday, November 11, 2011 4:41 PM
    Tuesday, November 8, 2011 7:32 PM
  • Thanks Cindy. I tried something like this but had some timing issues with some of the Word events firing. Will take another look though.
    Friday, November 11, 2011 4:42 PM
  • I realize that this is an old thread, but I am having the same problem.  Note that in Word 2010, this event kicks in as soon as a User clicks the File tab (even before closing or saving or running print preview). I have not been able to find a way to distinguish between VisibleChanged being fired because the User clicked the 'x' or because Word closes the task pane when you click the File tab.
    Tuesday, June 5, 2012 2:12 PM
  • I know this is not a proper solution, and would definitely be considered a "hack", but I've been struggling with this for many hours, and this is the best solution I've come up with. What I ended up doing was to capture the mouse cursor, and if it's in the upper-left corner of the window, then I assume the user has clicked the "File" tab. My situation is for an Outlook 2010 Add-In, so it may be slightly different, but I'm pretty sure the logic could be applied to any Office Add-In.

    Code from usercontrol:

            public Point GetMouseCursorPositionRelativeToOutlookWindow()
            {
                return this.PointToClient(Cursor.Position);
            }

    Code from ThisAddIn:

            private void taskPane_VisibleChanged(object sender, EventArgs e)
            {
                System.Drawing.Point p =userControl.GetMouseCursorPositionRelativeToOutlookWindow();
                bool userClickedFileTab = p.X < 100 && p.Y < 100;
                //your code here...
            }
    Wednesday, November 28, 2012 2:44 AM
  • I know this is a very old post put i just tried this approach and it works for the File menu but i would like to modify it so that it treats the File button the same as selecting a new document from the recent list.

    I have changed the boolean to
    bool userClickedFileTab = p.X < 800;

    which sort of works but the trouble is, if the user has made their application quite small, clicking on the X of the taskpane could also be less than 800, which makes it pointless. 

    I then wanted to change the 800 figure so it is say 3/4 of the screen width, but i cant reliably find a way. If i look at the application.width or the application.activewindow.width, they are about 1200 BUT the cursor x position when I click on the X is about 1500 (with or without the PointToClient). I must be missing something obvious here? 

    Wednesday, June 3, 2015 10:50 AM