Synchronizing an advanced task pane to a toggle button

Add-in Express™ Support Service
That's what is more important than anything else

Synchronizing an advanced task pane to a toggle button
Where can one add a handler for TaskPaneInstance.ADXAfterTaskPaneHide 
Alan Tolan




Posts: 8
Joined: 2016-04-15
I am trying to link a toggle button to an advanced custom task pane in Excel 2013

Connecting up the button is fine - I can find the task pane for current document using the TaskPaneInstance - but I want the toggle button to change state when the user manually closes the CTP.

There are a number of events on the TaskPaneInstance which look like they can be used for this but I cannot find a place to attach to them.

The various events on the AddInModule that look like good candidates



    this.OnTaskPaneBeforeCreate += HandleTaskPanelCreated;
    this.OnTaskPaneBeforeDestroy += HandleTaskPanelDestroyed;
    this.OnTaskPaneAfterShow += AddinModule_OnTaskPaneAfterShow;
    this.OnTaskPaneBeforeShow += AddinModule_OnTaskPaneBeforeShow;


seem to be not firing for me.


What am I missing?


NOTES:
I have a previous question on this which Andrei Smolin answered but the answer dealt with Custom Task Panes not advanced (at least the it seems this way - the VisibleStateChanged event used doesn't seem to be on the ACTP interface)


Thanks,
Alan.
Posted 07 Jun, 2017 04:59:04 Top
Andrei Smolin


Add-in Express team


Posts: 14127
Joined: 2006-05-11
Hello Alan,

Now you deal with an ADXExcelTaskPane, not ADXTaskPane, correct?

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 07 Jun, 2017 05:30:58 Top
Alan Tolan




Posts: 8
Joined: 2016-04-15
Yes,

I have two CTPs in my test app, both ADXExcelTaskPanes.

My understanding is that creation of instances of the task panes is handled by the ExcelTaskPanesManager and I should access them through the TaskPaneInstance and TaskPaneInstances properties of the ADXExcelTaskPanesCollectionItem

So, for example, to toggle the task pane I use


    ExcelPlainText.TaskPaneInstance.Visible = !ExcelPlainText.TaskPaneInstance.Visible



I can see the ADXAfterTaskPaneHide event on the ADXExcelTaskPane and it is firing but I cannot find an event to show me when an instance has been created, so I can't see where to attach a handler in order to pass a message back to the button.

If I attach to


   ExcelPlainText.TaskPaneInstance


in the AddInModule.AddinInitialize handler, the instance is null (and even if it wasn't, that would only handle the first document, not each subsequent one)

I tried adding handlers for some of the task pane events in the AddInModule


[CODE]
public AddinModule()
{

Application.EnableVisualStyles();

InitializeComponent();
// Please add any initialization code to the AddinInitialize event handler
this.AddinInitialize += HandleInitialize;

this.OnTaskPaneAfterCreate += AddinModule_OnTaskPaneAfterCreate;
this.OnTaskPaneAfterShow += AddinModule_OnTaskPaneAfterShow;

}

private void AddinModule_OnTaskPaneAfterShow(object sender, ADXTaskPane.ADXCustomTaskPaneInstance instance)
{
}

private void AddinModule_OnTaskPaneAfterCreate(object sender, ADXTaskPane.ADXCustomTaskPaneInstance instance, object control)
{
}


but as far as I can see they are not firing (or at least the debugger is not stopping in them)


I know that I am probably missing something simple but I can't see what it is.

Thanks,
Alan.
Posted 07 Jun, 2017 06:13:47 Top
Andrei Smolin


Add-in Express team


Posts: 14127
Joined: 2006-05-11
Let's use "CTP" to refer to Microsoft's Custom Task Panes technology; in Add-in Express, such task panes are represented with the ADXTaskPane class.

Alan Tolan writes:
this.OnTaskPaneAfterCreate += AddinModule_OnTaskPaneAfterCreate;
this.OnTaskPaneAfterShow += AddinModule_OnTaskPaneAfterShow;


These are CTP-related events.

You need to connect to the ADXExcelTaskPane.ADXAfterTaskPaneHide event. You can do this in the ADXExcelTaskPane.ADXBeforeTaskPaneShow (or ADXAfterTaskPaneShow) event.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 07 Jun, 2017 06:23:49 Top
Alan Tolan




Posts: 8
Joined: 2016-04-15
I have a handler in the TaskPane for ADXBeforeTaskPaneShow (I am using it to hide the Task Pane on load)



      
      public SusAdxExcelTaskPane()
      {
      
         this.ADXBeforeTaskPaneShow += HandleBeforeTaskPaneShow;
      }

      // hides the task pane on initial creation
      private void HandleBeforeTaskPaneShow(object sender, ADXBeforeTaskPaneShowEventArgs e)
      {

         this.ADXBeforeTaskPaneShow -= HandleBeforeTaskPaneShow;      
         Visible = false;
      }



So I can put a handler for ADXAfterTaskPaneHide here


      // hides the task pane on initial creation
      private void HandleBeforeTaskPaneShow(object sender, ADXBeforeTaskPaneShowEventArgs e)
      {

         this.ADXBeforeTaskPaneShow -= HandleBeforeTaskPaneShow;      
         Visible = false;
         this.ADXAfterTaskPaneHide += HandleTaskPaneHide;   
      }

      private void HandleTaskPaneHide(object sender, ADXAfterTaskPaneHideEventArgs e)
      {
         // do something on hide 
      }



But what do I do here? How do I invalidate the ToggleButton for this document?

Thanks
Alan.
Posted 07 Jun, 2017 08:31:45 Top
Andrei Smolin


Add-in Express team


Posts: 14127
Joined: 2006-05-11
Alan,

This works as follows. You start with invalidating the Ribbon button. When appropriate, Office invokes the callbacks that Add-in Express specified for your button. if you handle the PropertyChanging event of the Ribbon button, you can decide whether to show/hide a specific instance shown in the corresponding window. See also section Updating ribbon controls at run time at https://www.add-in-express.com/docs/net-ribbon-components.php#properties-events.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 07 Jun, 2017 10:11:29 Top
Alan Tolan




Posts: 8
Joined: 2016-04-15
Sounds good. How do I get a reference to the ribbon button?

The code for handling the event in inside the ADXExcelTaskPane.
The button is (by default) a private member of the AddinModule.

Is the preferred mechanism to make the member public and to cast the reference to the AddinModule in the TaskPane to my specific AddInModule and then access the button this way



     protected override void HandleTaskPaneHide(object sender, ADXAfterTaskPaneHideEventArgs e)
     {
         (AddinModule as Sus_AddInExpress_CombinedTest_Excel.AddinModule).TogglePlainText.Invalidate();
     }



or is there another mechanism that I should be using?


Thanks,
Alan.
Posted 07 Jun, 2017 10:45:52 Top
Andrei Smolin


Add-in Express team


Posts: 14127
Joined: 2006-05-11
You can create a public method invalidating that button. Call the method using this syntax:

{your project name such as MyAddin1}.AddinModule.CurrentInstance.{the method}().

MyAddin1.AddinModule.CurrentInstance.MyMethod().

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 07 Jun, 2017 10:58:13 Top