How to: Display Alerts and Dialog Boxes Using the InfoPath 2003 Object Model

When writing code to extend the functionality of a form template that uses the InfoPath 2003 object model, it is often useful to provide the user with information in a dialog box. Programmatically displaying a dialog box and related user interface elements is accomplished in InfoPath by using the methods of the UIObject interface.

Overview of the UIObject Interface

The UIObject interface provides the following methods, which form developers can use to have different types of dialog boxes displayed to InfoPath users as they are filling out a form.

Name Description


Displays a simple message box that contains a specified message string. This method should be used when no input is required from the user and only a message needs to be displayed. The dialog box displayed is closed by clicking the OK button.


Displays a message box with buttons for input from a user. The value that is returned is one of the XdConfirmChoice enumerated constants.


Sets the default file name for a form in the Save As dialog box.


Sets the initial location at which the Save As dialog box starts to browse when it is opened.


Creates a new e-mail message in the default e-mail application, with the currently open form attached to the message.


Displays a modal dialog box, based on the specified .html file and positional arguments. This method should be used if you want to display more than a simple message to the user and you need to get back some data from the user (beyond the simple confirmation that is provided by the Yes | No | Cancel buttons displayed by the Confirm method).


Displays the built-in Digital Signatures dialog box.

Using the UIObject Interface

The UIObject interface is accessed through the UI property of the XDocument interface, which itself is accessed through the thisXDocument variable that is initialized in the _Startup method of the form code class. The following example demonstrates using the ShowMailItem and Alert methods of the UIObject interface.

thisXDocument.UI.ShowMailItem("","", "", 
   "Updated Form", "Here is the updated form that you requested.");

thisXDocument.UI.Alert("The e-mail message has been created.");

Using the ShowModalDialog Method

This example demonstrates how to use the ShowModalDialog method of the UIObject interface to display a custom dialog box defined in the HTML file show.html.

public void CTRL1_5_OnClick(DocActionEvent e)
   // Write your code here.

Both the Visual C# and Visual Basic samples depend on an HTML file named "show.html" that defines the dialog box that is invoked by the ShowModalDialog method. This HTML file displays some data from the form and shows a text box for the user to fill in a value. The value in the textbox is returned to the form when the dialog box is closed.

      <script language="JScript">
function BtnClick()
   xdocument = window.dialogArguments;
   myXml = xdocument.DOM.xml
   aForm = oForm.elements;
   aForm.textBox.value = myXml;
      <H1><FONT face="Arial">This is a modal dialog box</FONT> &nbsp;
      <BUTTON onclick="BtnClick()" id="BUTTON1" type="button">
         Get XML DOM
      <FORM ID="oForm">
         <INPUT Type="text" name="textBox">


The ShowModal method requires Full Trust to run or preview. For more information, see How to: Preview and Debug Managed Code Form Templates that Require Full Trust.

Using a Windows Form for a Dialog Box

Instead of using an HTML file to show the contents in a dialog box, as shown in the previous example, you can also display a Windows Form as a dialog box from your InfoPath form. This example demonstrates how to display a Windows Form as a custom dialog box from a button named ShowDialog on an InfoPath form. This does not require using the UIObject interface or any other members of the InfoPath object model.


You cannot create and add a Windows Form to an InfoPath form template project created using Microsoft Visual Studio Tools for Applications (VSTA). To create and add a Windows form to a form template project, you must be using Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System and Visual Studio 2005 or Visual Studio 2008 with Visual Studio Tools for Office.

public void ShowDialog_OnClick(DocActionEvent e)
   // Instantiate and show Form1.
   Form1 myform = new Form1();

Both the Visual C# and Visual Basic samples depend on a Windows Form named "Form1" that defines the dialog box that is invoked by the event handler for the ShowDialog button on the InfoPath form. To add Form1 to your InfoPath project, use the following procedure.

Add a Windows Form to an InfoPath project

  1. In the Solution Explorer, right-click the form code project (projectnameFormCode), point to Add, and then click Add Windows Form.

  2. In the Add New Item dialog box, name the form, and then click Open.

  3. From the Toolbox, drag a Button control onto the form.

  4. Double-click the button, and then add the following code to the button's event handler.

   // Close the form.