How to: Work with Form Windows

Applies to: InfoPath 2010 | InfoPath Forms Services | Office 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio Tools for Microsoft Office

In this article
Overview of the WindowsCollection Class
Overview of the Window Class
Using the WindowsCollection and Window Classes

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 object model provided by the Microsoft.Office.InfoPath namespace supports access to a form's windows through the use of the Window class in association with the WindowCollection class.

Note

The classes for working with a form's windows are available only when working with an InfoPath Editor Form. If a form template's Compatibility setting is Web Browser Form, these classes are not available.

There are two types of windows in InfoPath:

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

  • The designing window that 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 Window object that represents the current window to access a variety of properties and methods that can be used to customize the form editing experience.

Overview of the WindowsCollection Class

The WindowCollection class provides the following properties, which form template developers can use to manage the Window objects that it contains.

Name

Description

Count property

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

Item property

Gets a reference to the specified Window object.

Overview of the Window Class

The Window class 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 (WindowType) you are working with. Some methods and properties work only with the editor window type (WindowType.Editor). The remaining methods and properties work with both the editor window type and the designer window type (WindowType.Designer). 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

Activates (gives focus to) the window.

Both Designer and Editor type

Active property

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

Both Designer and Editor type

Caption property

Gets or sets the caption text for the window represented by the Window object.

Only Editor type

Close() method

Closes the window prompting to save changes to any unsaved form, or form with changes that have not been saved.

Only Editor type

Close(Boolean) method

Closes the window and optionally forces an unsaved form or form with unsaved changes to be closed without saving.

Only Editor type

CommandBars property

Gets a reference to the Microsoft Office CommandBars collection that is associated with the window.

Both Designer and Editor type

Height property

Gets or sets the height of the window, measured in points.

Both Designer and Editor type

Left property

Gets or sets the horizontal position of the window, measured in points.

Both Designer and Editor type

MailEnvelope property

Gets a reference to the MailEnvelope class.

Only Editor type

TaskPanes property

Gets a reference to the TaskPaneCollection collection.

Both Designer and Editor type

Top property

Gets or sets the vertical position of the window, measured in points.

Both Designer and Editor type

Width property

Gets or set the width of the window, measured in points.

Both Designer and Editor type

WindowState property

Gets or sets the state of the window as a WindowState value.

Both Designer and Editor type

WindowType property

Gets the type of the window as an WindowType enumeration value.

Both Designer and Editor type

XmlForm property

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

Only Editor type

Using the WindowsCollection and Window Classes

The WindowCollection class can be accessed through the Windows property of the Application class. When using the WindowCollection class 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 Window object instance. For example, the following code sets a reference to the first Window object contained in the WindowCollection for the current InfoPath session.

Window myWindow = this.Application.Windows[0];
Dim myWindow As Window = Me.Application.Windows(0)

You can access the currently open window directly using the ActiveWindow property of the Application class, without going through the WindowCollection, as shown in the following line of code.

Window myWindow = this.Application.ActiveWindow;
Dim myWindow As Window = Me.Application.ActiveWindow

A Window object can also be accessed by using the Window property of the View class, which represents the current view that is being used to work with the form's underlying XML document. The CurrentView property of the XmlForm class is used to access a View object that represents the current view. For example, the following code sets a reference to the Window that is associated with the current view.

Window myWindow = this.CurrentView.Window;
Dim myWindow As Window = Me.CurrentView.Window

Note

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