CTP Custom Task Pane Close Event

59 views
Skip to first unread message

Michael Goldsworth

unread,
Apr 25, 2023, 7:34:48 PM4/25/23
to Excel-DNA
I am not sure there is an easy solution to this, but I imagine someone else here has experienced the same issue, and I'm having trouble finding solutions online that work.

I'm leverage Custom Task Panes for different types of user interaction and have a case where while executing an event processing method need to stop and request input from the user. To deal with this, I put two panels in a Custom Task Pane and hide one or the other as needed. The result of this is that I now also need to allow the event processing method to return before the user can click on buttons in the second panel that is displayed. All of that is more or less fine.

However I also end up in this situation now where a user can click the 'X' button in the upper left-hand corner of the Custom Task Pane and close it altogether, and I can't find any native event handler that gets triggered on the CTP itself when this happens.

Is there some parent-level control or something else that allows for capturing that behavior? If not, is there some way to prevent the 'X' and CTP header section to display or be visible to the user? If I have to I suppose I can add some sort of reset button in the second panel that will restore state when the CTP is re-opened, but it's not ideal or especially clean :/

Thank you very much,
Mickey

Michael Goldsworth

unread,
Apr 25, 2023, 8:13:06 PM4/25/23
to Excel-DNA
Ohhhh ... okay, figured this out. Hopefully this will continue to work and be useful for others, and event handlers don't get updated somewhere unexpectedly, but it would be good to confirm.

It isn't sufficient to implement a _VisibleChanged(object sender, EventArgs e) event handler within the CTP form itself, as it won't get called when it is closed using the 'X' or the Close option from the CTP dropdown.

From examples here in the group, I implemented a CTPManager class, only using a dictionary that houses all the created CTPs rather than a single object, then on the ShowCTP() method takes an enum for the Custom Task Pane I want, created a new one if it's not already in the dictionary, make all existing CTPs invisible, then make the CTP to show visible.

        private static readonly Dictionary<TaskPaneForm, CustomTaskPane> ctps = new();

        public static void ShowCTP(TaskPaneForm taskPaneForm)
        {
            if (!ctps.ContainsKey(taskPaneForm))
            {
                CTP ctp = ctpm[taskPaneForm];
                ctps.Add(taskPaneForm, CustomTaskPaneFactory.CreateCustomTaskPane(ctp.Type, ctp.Header));
                ctps[taskPaneForm].Width = 400;
            }

Now in the ITaskPaneForm interface class, I added this method to be implemented by every Custom Task Pane form:

        public void VisibleStateChanged(CustomTaskPane CustomTaskPaneInst);

And on creation of a new CTP form using the CustomTaskPaneFactory.CreateCustomTaskPane() method, I follow it with:

        ITaskPaneForm ctpf = (ITaskPaneForm)ctps[taskPaneForm].ContentControl;
        ctps[taskPaneForm].VisibleStateChange += ctpf.VisibleStateChanged;

Then each Customer Task Pane form method implementing this can have it's own visible changed event handler. Unlikely the normal CTP form _VisibleChanged() event, his method *does* get called when the 'X' is clicked, and I now have a way to know when I need to reset form data, cancel the current operation, and cleanup any objects in memory I no longer need there.

Thanks,
Mickey
Reply all
Reply to author
Forward
0 new messages