Task panes don’t have a dedicated visual designer because they are implemented through the creation of a user control, which already has a design surface. To add a custom task pane to your Word add-in, right-click the project, select Add New Item, and then select the Windows Forms User Control item.
Because actions panes are document-level concepts, you’ll read about those separately in the section “Creating an Office Document Extension,” later in this chapter. You follow the same general development process.
After the user control is added and the designer is loaded, you can set about creating the UI and code-behind for the task pane. The only Office-specific action item here is wiring the task pane user control into Word’s object model. All that work is accomplished in code within the add-in class. First, to make life a bit easier, you add a using
statement to your add-in class (in this case, the ThisAddIn
class).
using Microsoft.Office.Tools;
Then you declare two local objects—one for the task pane and one for the user control.
private PurchaseOrderTaskControl poUserControl;
private CustomTaskPane poTaskPane;
Finally, you need the code to add the custom task pane to the application instance. You put this in the Startup
event (for this example, ThisAddIn_Startup
) so that the task pane is immediately available and visible when you run the add-in.
poUserControl = new PurchaseOrderTaskControl();
poTaskPane = this.CustomTaskPanes.Add(poUserControl, "Purchase Orders");
poTaskPane.Visible = true;
If you build and run the project now, you should see your task pane within the Word environment (see Figure 22.10).
Tip
The preceding example uses a Windows Forms user control. If you want to create your task pane using Windows Presentation Foundation (WPF) instead, you simply add a WPF user control to the project. Everything from a design and coding experience would work the same. Behind the scenes, Visual Studio will automatically create a System.Windows.Forms.Integration.ElementHost
object and use that to parent/host your WPF controls within the targeted Office application.