C# .Net – How to capture a close button press on a Office Custom Task pane


When developing our office custom task pane we wanted to control whether pane was displayed when the office application started.

Initially we implemented a ribbon menu that toggled the panes visibility. When the user toggled the pane, we stored the visibility in the registry so that the next time the office application started we would only show the pane if it was required.

But, if the user closed the pane via its close button rather than the toggle button then we would not update the registry.

The solution was to update the registry when the pane visibility changed rather than when the toggle method is called.

There are a couple of events exposed on _CustomTaskPaneEvents_Event interface:

  1. DockPositionStateChange
  2. VisibleStateChange

The VisibleStateChange event is fired when ever the pane shows or is removed.

public void CTPFactoryAvailable( ICTPFactory CTPFactoryInst ) {
    CTP = CTPFactoryInst.CreateCTP(
        typeof( OfficeTaskPanelControl ).FullName,
        "Perfion", Type.Missing );

CTP.VisibleStateChange += CTP_VisibleStateChange;

    CTP.Visible = true;
    CTP.Width = 250;
}

void CTP_VisibleStateChange( CustomTaskPane CustomTaskPaneInst ) {
    // Update the registry at this point. RegHlp.Set("PanelVisibility", "" + CTP.Visible );
}
Advertisements

Tags: , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: