Adding Controls to the Task Pane

Adding controls to the task pane is very similar to adding controls to a Windows Form. Using VSTO, you add controls to the ActionsPane object for Word 2003 and Excel 2003 solutions. If you're using VSTO 2005 SE and Office 2007, you can add the controls to the Custom task pane, which is available to any document you have opened. The remainder of this section describes controls on the actions pane, but it is also applicable to the Custom task pane. In Chapter 14 you'll learn more about adding controls to the Custom task pane.

In the preceding section, you added controls to a user control and then added the user control to a Windows Form. Instead, you can add the user control directly to the task pane. In this way, you can provide a user interface that does not take up a lot of space on the document nor hide any of the document's contents.

First, you change the code so that instead of adding the user control to a Windows Form and then displaying the Windows Form, you add the user control to the actions pane.

1. Right-click ThisDocument.vb, and select View Code.

2. Replace the code in the Click event handler of the CreateLogo button with the code in Listing 8.13.

Listing 8.13. Displaying a user control on the task pane when a menu item is clicked

Private Sub CreateLogo_Click(ByVal Ctrl As _ Microsoft.Office.Core.CommandBarButton, _ ByRef CancelDefault As Boolean) Handles CreateLogo.Click

Dim myUserControl As New UserControl1

If Me.ActionsPane.Controls.Count < 1 Then

Me.ActionsPane.Controls.Add(myUserControl) End If

Me.Application.TaskPanes(Word.WdTaskPanes. _ wdTaskPaneDocumentActions).Visible = True

End Sub

This code first checks whether there are any controls on the actions pane before adding the user control. Without the If statement, the controls would be added to the document each time the menu item is clicked. There is also code to set the Visible property of the Document Actions task pane to True. This is needed because the task pane is automatically made visible only the first time you add a control to it. If the end user closes the task pane and then clicks the CreateLogo button again, it would not automatically become visible. Therefore, we add code to make sure the task pane is visible whenever this button is clicked.

Press F5 to run the code. This time the controls are displayed on the Document Actions task pane, and when you add text and a graphic, the header should resemble the document in Figure 8.14.

Any Windows Forms control that can be added to a Windows Form can also be added to the actions pane. You can add the control directly to the Controls collection of the ActionsPane, or, as in this example, you can first add controls to a user control and then add it to the ActionsPane's Control collection. For more about customizing the Document Actions task pane, see Chapter 5.

Now that you know how to display controls on a Windows Form and on the Document Actions task pane, we will take a look at adding controls directly to the document.

<* L' [wen FeirMt I«lf Ttf* i™ - " Het r^« N*. .

a / o- ijjjii J" ■ wm.»^ ■ u - » , u g« = ;= it |

<* L' [wen FeirMt I«lf Ttf* i™ - " Het r^« N*. .

a / o- ijjjii J" ■ wm.»^ ■ u - » , u g« = ;= it |

Figure 8.14. Adding a logo and text to the first-page header via the actions pane
0 0

Post a comment