Control Type

When you add a Windows Forms control to a document, VSTO creates its version of the Windows Forms control. Instead of the control being in the System.Windows.Forms namespace, it is in the Microsoft.Office.Tools.Word.Controls namespace or the Microsoft.Office.Tools.Excel.Controls namespace, depending on which application you're using.

The document (or worksheet) does not know anything about Windows Forms controls or their behaviors. ActiveX controls are the only thing that documents are aware of. Therefore, the Windows Forms control is not actually placed directly on the document; instead, the control is placed inside an ActiveX control, which in turn is placed on the document.

Actually, there is a third layer of indirection called the ContainerControl. The ActiveX control is hosted on the document and contains the ContainerControl, which contains VSTO's version of the Windows Forms control. But all you and your users see is the Windows Forms control. There is evidence of this, however, in an Excel worksheet. When you add a control, such as a Button, to the worksheet at design time, you'll notice that the formula bar contains the following text, as shown in Figure 8.28:

=EMBED("WinForms.Control.Host","")

This formula should not be deleted or altered in any way.

Figure 8.28. Controls on a worksheet

Some of the behaviors of the control on a document differ from those of controls on a Windows Form. For example, if you set the positioning properties of the control, they are really being set in relationship to their parent object—the ActiveX control (or ContainerControl)—rather than the document, as you might expect.

One more thing you should keep in mind is that although we've listed the types of Windows Forms controls that are available on the Toolbox when the document is the designer—and some of the controls were removed—this doesn't mean that you cannot add these controls to your document. It just means that you cannot add them to the document surface at design time. VSTO provides a way for you to add these controls to the document at run time by using the AddControl helper method.

0 0

Post a comment