How to: Work with Form Windows Using the InfoPath 2003 Object Model

When working programmatically with an InfoPath form, you can write code to access the form's windows, and then customize some of the items that they contain. The InfoPath 2003-compatible object model supports access to a form's windows through the use of the WindowObject interface in association with the WindowsCollection interface.

There are two types of windows in InfoPath:

  • The editing window, which is used when a user fills out a form.

  • The designing window, which is used when a user designs a form template.

When writing code in a form template, it is the editing window that provides the most useful functionality, because you can use a WindowObject instance that references it to access a variety of properties and methods that can be used to customize the form editing experience.

Overview of the WindowsCollection Interface

The WindowsCollection interface provides the following properties, which form template developers can use to manage the WindowObject instances that it contains.

Name Description

Count property

Returns a count of the number of Window objects contained in the collection.

Item property

Returns a reference to the specified Window object.

NoteNote:
Visual C# accesses collections using an indexer instead of calling the Item property. For example, thisApplication.Windows[0].Caption.

Overview of the Window Object

The WindowObject interface provides the following methods and properties, which form developers can use to interact with an InfoPath window. Support for these methods and properties differ depending on the type of window (XdWindowType) you are working with. Some methods and properties work only with the editor window type (XdWindowType.xdEditorWindow). The remaining methods and properties work with both the editor window type and the designer window type (XdWindowType.xdDesignerWindow). Additionally, like all InfoPath object model members, when called from a form template, support for methods and properties can vary depending on the security level and how the form is deployed.

Name Description Window Type Support

Activate method

Designates the window as the currently active window.

Both the xdDesignWindow and xdEditorWindow types

Active property

Returns a Boolean value indicating whether the window is the currently active window.

Both the xdDesignWindow and xdEditorWindow types

Caption property

A read/write property that returns or sets the caption text for the window represented by the Window object.

Only the xdEditorWindow type

Close method

Closes a window.

Only the xdEditorWindow type

CommandBars property

Returns a reference to the Microsoft Office CommandBars object.

Both the xdDesignWindow and xdEditorWindow types

Height property

A read/write property of type long integer that specifies the height of the window represented by the Window object, measured in points.

Both the xdDesignWindow and xdEditorWindow types

Left property

A read/write property of type long integer that specifies the horizontal position of the window represented by the Window object, measured in points.

Both the xdDesignWindow and xdEditorWindow types

MailEnvelope property

Returns a reference to the MailEnvelopeObject object.

Only the xdEditorWindow type

TaskPanes property

Returns a reference to the TaskPanesCollection collection.

Both the xdDesignWindow and xdEditorWindow types

Top property

A read/write property of type long integer that specifies the vertical position of the window represented by the Window object, measured in points.

Both the xdDesignWindow and xdEditorWindow types

WindowType property

Returns a number indicating the type of the window, based on the XdWindowType enumeration.

Both the xdDesignWindow and xdEditorWindow types

Width property

A read/write property of type long integer that specifies the width of the window represented by the Window object, measured in points.

Both the xdDesignWindow and xdEditorWindow types

WindowState property

A read/write property of type XdWindowState that returns or sets the state of the window represented by the Window object.

Both the xdDesignWindow and xdEditorWindow types

XDocument property

Returns a reference to the _XDocument object associated with the window.

Only the xdEditorWindow type

Using the WindowsCollection and Window Interfaces

The WindowsCollection interface can be accessed through the Windows property of the Application interface. When using the WindowsCollection interface to access a form's windows, you use an indexer (for Visual C#) or pass a long integer to the Item property (for Visual Basic) to return a reference to a WindowObject interface instance. For example, the following code sets a reference to the first WindowObject contained in the WindowsCollection.

WindowObject objWindow = thisApplication.Windows[0];

However, you can access the currently open window directly by using the ActiveWindow property of the Application interface, without going through the WindowsCollection, as the following code demonstrates.

WindowObject objWindow = thisApplication.ActiveWindow;
NoteNote:

When debugging an InfoPath managed-code project, the ActiveWindow property will always return null because the debugging window is active.

A WindowObject can also be accessed by using the Window property of the View interface, which is associated with the form's underlying XML document. The View property of the XDocument interface is used to access the View object. For example, the following code sets a reference to the WindowObject that is associated with the view of a form's underlying XML document.

WindowObject objWindow = thisXDocument.View.Window;
NoteNote:

Some of the properties and methods of the Window object are only for the editing window type; if used with the designing window type, they will return an error. The properties and methods that are supported for each window type are listed in the table shown earlier in this topic. You can use the WindowType property in your code to determine which type of window you are working with.

Show: