
Creating a Class to Manage Inspector Windows and Custom Task Panes
There are several cases in which the add-in must identify which custom task pane is associated with a specific e-mail message. These cases include the following:
When the user closes an e-mail message. In this case, the add-in must remove the corresponding custom task pane to ensure that resources used by the add-in are cleaned up correctly.
When the user closes the custom task pane. In this case, the add-in must update the state of the toggle button on the Ribbon of the e-mail message.
When the user clicks the toggle button on the Ribbon. In this case, the add-in must hide or display the corresponding task pane.
To enable the add-in to keep track of which custom task pane is associated with each open e-mail message, create a custom class that wraps pairs of Inspector and CustomTaskPane objects. This class creates a new custom task pane object for each e-mail message, and it deletes the custom task pane when the corresponding e-mail message is closed.
To create a class to manage Inspector windows and custom task panes
In Solution Explorer, right-click the ThisAddIn.cs or ThisAddIn.vb file, and then click View Code.
Add the following statements to the top of the file.
|
Imports System.Collections.Generic
Imports Microsoft.Office.Tools
Imports Office = Microsoft.Office.Core
Imports Outlook = Microsoft.Office.Interop.Outlook
|
|
using Microsoft.Office.Tools;
|
Add the following code to the ThisAddIn.cs or ThisAddIn.vb file, outside the ThisAddIn class (for Visual C#, add this code inside the OutlookMailItemTaskPane namespace). The InspectorWrapper class manages a pair of Inspector and CustomTaskPane objects. You will complete the definition of this class in the following steps.
|
Public Class InspectorWrapper
Private inspector As Outlook.Inspector
Private WithEvents inspectorEvents As Outlook.InspectorEvents_Event
Private WithEvents taskPane As CustomTaskPane
|
|
public class InspectorWrapper
{
private Outlook.Inspector inspector;
private CustomTaskPane taskPane;
|
Add the following constructor after the code that you added in the previous step. This constructor creates and initializes a new custom task pane that is associated with the Inspector object that is passed in. In C#, the constructor also attaches event handlers to the Close()()() event of the Inspector object and to the VisibleChanged event of the CustomTaskPane object.
|
Public Sub New(ByVal Inspector As Outlook.Inspector)
Me.inspector = Inspector
inspectorEvents = TryCast(Me.inspector, Outlook.InspectorEvents_Event)
taskPane = Globals.ThisAddIn.CustomTaskPanes.Add(New TaskPaneControl(), _
"My task pane", Inspector)
End Sub
|
|
public InspectorWrapper(Outlook.Inspector Inspector)
{
inspector = Inspector;
((Outlook.InspectorEvents_Event)inspector).Close +=
new Outlook.InspectorEvents_CloseEventHandler(InspectorWrapper_Close);
taskPane = Globals.ThisAddIn.CustomTaskPanes.Add(
new TaskPaneControl(), "My task pane", inspector);
taskPane.VisibleChanged += new EventHandler(TaskPane_VisibleChanged);
}
|
Add the following method after the code that you added in the previous step. This method is an event handler for the VisibleChanged event of the CustomTaskPane object that is contained in the InspectorWrapper class. This code updates the state of the toggle button whenever the user opens or closes the custom task pane.
|
Private Sub TaskPane_VisibleChanged(ByVal sender As Object, ByVal e As EventArgs) _
Handles taskPane.VisibleChanged
Globals.Ribbons(inspector).ManageTaskPaneRibbon.ToggleButton1.Checked = taskPane.Visible
End Sub
|
|
void TaskPane_VisibleChanged(object sender, EventArgs e)
{
Globals.Ribbons[inspector].ManageTaskPaneRibbon.toggleButton1.Checked =
taskPane.Visible;
}
|
Add the following method after the code that you added in the previous step. This method is an event handler for the Close()()() event of the Inspector object that contains the current e-mail message. The event handler frees resources when the e-mail message is closed. The event handler also removes the current custom task pane from the CustomTaskPanes collection. This helps prevent multiple instances of the custom task pane when the next e-mail message is opened.
|
Sub InspectorWrapper_Close() Handles inspectorEvents.Close
If Not (taskPane Is Nothing) Then
Globals.ThisAddIn.CustomTaskPanes.Remove(taskPane)
End If
taskPane = Nothing
Globals.ThisAddIn.InspectorWrappers.Remove(inspector)
RemoveHandler inspectorEvents.Close, AddressOf InspectorWrapper_Close
inspector = Nothing
End Sub
|
|
void InspectorWrapper_Close()
{
if (taskPane != null)
{
Globals.ThisAddIn.CustomTaskPanes.Remove(taskPane);
}
taskPane = null;
Globals.ThisAddIn.InspectorWrappers.Remove(inspector);
((Outlook.InspectorEvents_Event)inspector).Close -=
new Outlook.InspectorEvents_CloseEventHandler(InspectorWrapper_Close);
inspector = null;
}
|
Add the following code after the code that you added in the previous step. Later in this walkthrough, you will call this property from a method in the custom Ribbon UI to display or hide the custom task pane.
|
Public ReadOnly Property CustomTaskPane() As CustomTaskPane
Get
Return taskPane
End Get
End Property
End Class
|
|
public CustomTaskPane CustomTaskPane
{
get
{
return taskPane;
}
}
}
|