Walkthrough: Creating and Debugging a Basic Form Template Using the InfoPath 2003 Object Model

This topic provides a walkthrough of creating a basic InfoPath managed code form template that works with the InfoPath 2003-compatible object model provided by the Microsoft.Office.Interop.InfoPath.SemiTrust namespace.

Hello World

In the following example, you will learn how to display a simple alert dialog box by using the Alert method of the InfoPath 2003-compatible object model.

Create a new InfoPath form template that works with the InfoPath 2003-compatible object model

  1. Create a new form template that works with the InfoPath 2003-compatible object model, as described in How to: Create a Managed Code Form Template Using the InfoPath 2003 Object Model.

  2. Name the form template project HelloWorld and save it.

    The project system creates code and project files, and then opens a blank form template in InfoPath design mode. You are now ready to add event handlers.

Add an OnClick event handler

  1. Click Controls on the Design Tasks task pane.

  2. If you are working in InfoPath with Microsoft Visual Studio Tools for Applications (VSTA), in the Standard controls section, click the Button control to insert it into the view.

    If you are working in Visual Studio 2005 with Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System or Visual Studio 2008 with Visual Studio Tools for Office, drag the Button control from the Toolbox onto the manifest.xsf [Design] form designer.

  3. Double-click the button to open the Button Properties dialog box.

  4. Change the Label to Alert.

  5. Change the ID to AlertID.

  6. Click Edit Form Code.

    An event handler skeleton for the OnClick event is created and the focus moves to the code editor in VSTA or Visual Studio. For more information on working with event handlers, see How to: Add an Event Handler Using the InfoPath 2003 Object Model.

    You are now ready to add form code to the event handler for the button.

Add form code to the event handler

  1. In the OnClick event handler, type the following code:

    thisXDocument.UI.Alert("Hello World!");
    
    thisXDocument.UI.Alert("Hello World!")
    

    Note that a Microsoft IntelliSense drop-down list is displayed after you type each period in the line of code. The entire event handler should look like the following:

    [InfoPathEventHandler(MatchPath="AlertID", EventType=InfoPathEventType.OnClick)]
    public void AlertID_OnClick(DocActionEvent e)
    {
        thisXDocument.UI.Alert("Hello World!");
    }
    
    <InfoPathEventHandler(MatchPath:="AlertID", EventType:=InfoPathEventType.OnClick)>
    Public Sub AlertID_OnClick(ByVal e As DocActionEvent)
        thisXDocument.UI.Alert("Hello World!")
    End Sub
    

    Note

    As an alternative to using the Alert method, you can use the MessageBox.Show method of the System.Windows.Forms namespace to display a message box. To do so, you must add a reference to the System.Windows.Forms assembly, add using System.Windows.Forms; or Imports System.Windows.Forms to the directives at the beginning of your code file, and then type a line of code such as the following: MessageBox.Show("Hello World!); or MessageBox.Show("Hello World!)

  2. If you are working with InfoPath and VSTA, switch to the InfoPath design mode window, and then click the Preview button on the Standard toolbar.

    If you are working in Visual Studio, on the Debug menu click Start Debugging (or press F5).

  3. In the Preview window, click the Alert button.

    A message box will be displayed with the text "Hello World!"

    The next procedure shows how to add debugging breakpoints to your form code.

Debug form code

  1. In the code editor, click the grey bar to the left of the line:

    thisXDocument.UI.Alert("Hello World!");
    
    thisXDocument.UI.Alert("Hello World!")
    

    A red circle is displayed and the line of code is highlighted to indicate that the runtime will pause at this breakpoint in your form code.

  2. On the Debug menu, click Start Debugging (or press F5).

  3. In the InfoPath Preview window, click the Alert button.

    The code editor is given focus, and the breakpoint line is highlighted.

  4. On the Debug menu, click Step Over (or press F10) to continue stepping through the code.

    The Alert method code is executed, and the "Hello World!" alert is displayed in the InfoPath Preview window.

Getting the Current User's Name

By using the .NET Framework classes, you can get access to functionality that was not easily available in script. In this example, you will learn how use the .NET Framework classes to retrieve the name of the current user.

Add an OnLoad event handler

  1. Open the InfoPath HelloWorld project that you created earlier.

  2. On the View menu, click Data Source.

  3. Right-click the myFields node, and then click Add.

  4. In Name, type employee, then click OK.

  5. Drag the employee node into the view.

  6. If you are working in InfoPath with VSTA, on the Tools menu, click Programming, and then click On Load Event.

    If you are working in Visual Studio, on the Insert menu, click Event, and then click On Load Event.

    This will create an event handler for the OnLoad event, and focus moves to the code editor. The code in this event handler will be called each time the form is loaded. The next procedure shows how to add form code that retrieves the user's name to the event handler.

Add form code

  1. In the OnLoad event handler, type the following code:

    // Store an XML DOM node as a local variable.
    IXMLDOMNode nodeEmployee = thisXDocument.DOM.selectSingleNode("my:myFields/my:employee");
    if(nodeEmployee != null)
    {
        if(nodeEmployee.text == "")
        {
        // If the employee name is blank when the form is loaded, 
        // populate the employee node with the current user name.
        nodeEmployee.text = System.Environment.UserName;
        }
    }
    
    // Store an XML DOM node as a local variable.
    Dim nodeEmployee As IXMLDOMNode
    nodeEmployee = thisXDocument.DOM.selectSingleNode("my:myFields/my:employee");
    If Not(nodeEmployee Is Nothing) Then
        If(nodeEmployee.text = "") Then
        // If the employee name is blank when the form is loaded, 
        // populate the employee node with the current user name.
        nodeEmployee.text = System.Environment.UserName
        End If
    End If
    
  2. Compile and preview the form.

    The employee text box should now be populated with your username.

For information on how to deploy a managed code form template, see How to: Deploy InfoPath Projects. For information on the InfoPath object model and common programming tasks in managed code form templates that work with the InfoPath 2003-compatible object model, see Understanding the InfoPath 2003 Object Model.

See Also

Tasks

How to: Add an Event Handler Using the InfoPath 2003 Object Model

Concepts

Initialization and Clean-up Code Using InfoPath 2003 Object Model
InfoPath 2003 Compatible Object Models