Walkthrough: Creating a Basic Form Template with Code

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

In Microsoft InfoPath 2010, you can write business logic in Visual Basic or C# by opening a form template in the InfoPath designer, and then using one of the user interface commands to add an event handler, which will open the Microsoft Visual Studio Tools for Applications development environment for writing your code. By default, form template projects created using Microsoft Visual Studio Tools for Applications work against the managed code object model provided by the Microsoft.Office.InfoPath namespace.

This walkthrough first shows you how to create a simple Hello World application using C# or Visual Basic in the Microsoft Visual Studio Tools for Applications development environment. The walkthrough concludes with a code sample that shows you how to use the UserName property of the User class to retrieve the current user's name and populate a Text Box control with that value.

Prerequisites

In order to complete this walkthrough using the Microsoft Visual Studio Tools for Applications development environment, you will need:

  • Microsoft InfoPath 2010 with Microsoft Visual Studio Tools for Applications installed.

Hello World in Visual Studio Tools for Applications

In the following walkthrough, you will learn how to write code in the Microsoft Visual Studio Tools for Applications development environment to display a simple alert dialog box by writing an event handler for the Clicked event of the ButtonEvent class, which is associated with the Button control.

Create a new project and specify the programming language

  1. Start the InfoPath designer, and then double-click the Blank (InfoPath Editor) form template.

  2. To specify which programming language to use, click the Office Button, click Form Options, click Programming in the Category list, and then select either Visual Basic or C# from the Form template code language drop-down list.

    Note

    The other programming language options in the Form template code language drop-down list provide compatibility with previous versions of InfoPath. The C# (InfoPath 2007 Compatible) and Visual Basic (InfoPath 2007 Compatible) options will work with the procedures in this topic. However, to use the C# (InfoPath 2003 Compatible) and Visual Basic (InfoPath 2003 Compatible) options, see Walkthrough: Creating and Debugging a Basic Form Template Using the InfoPath 2003 Object Model.

    You are now ready to add a Button control and create its event handler.

Add a Button control and event handler

  1. In the Controls group, click the Button control to add it the form.

  2. Double-click the Button control, type Hello for the Label property on the Properties tab of the ribbon, and then click Custom Code. When prompted, save the form and name it HelloWorld.

    This will open the Visual Studio Tools for Applications environment with the cursor in the event handler for the Clicked event of Button control.

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

Add "Hello World" code to the event handler and preview the form

  1. In the event handler skeleton, type:

    MessageBox.Show("Hello World!");
    
    MessageBox.Show("Hello World!")
    

    The code for your form template should look similar to the following:

    using Microsoft.Office.InfoPath;
    using System;
    using System.Windows.Forms;
    using System.Xml;
    using System.Xml.XPath;
    
    namespace HelloWorld
    {
       public partial class FormCode
       {
          public void InternalStartup()
          {
             ((ButtonEvent)EventManager.ControlEvents["CTRL1_5"]).Clicked += new ClickedEventHandler(CTRL1_5_Clicked);
          }
    
          public void CTRL1_5_Clicked(object sender, ClickedEventArgs e)
          {
             MessageBox.Show("Hello World!");
          }
       }
    }
    
    Imports Microsoft.Office.InfoPath
    Imports System
    Imports System.Windows.Forms
    Imports System.Xml
    Imports System.Xml.XPath
    
    Namespace HelloWorld
       Public Class FormCode
          Private Sub InternalStartup(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Startup
             AddHandler DirectCast(EventManager.ControlEvents("CTRL1_5"), ButtonEvent).Clicked, AddressOf CTRL1_5_Clicked
          End Sub
    
          Public Sub CTRL1_5_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
             MessageBox.Show("Hello World!")
          End Sub
       End Class
    End Namespace
    
  2. Switch to the InfoPath designer window.

  3. Click the Preview button on the Home tab.

  4. Click the Hello button on the form.

    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. Switch back to the Microsoft Visual Studio Tools for Applications window.

  2. Click the grey bar to the left of the line:

    MessageBox.Show("Hello World!");
    
    MessageBox.Show("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.

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

  4. In the InfoPath Preview window, click the Hello button on the form.

  5. The Microsoft Visual Studio Tools for Applications code editor is given focus, and the breakpoint line is highlighted.

  6. On the Debug menu, click Step Over (or press Shift+F8) to continue stepping through the code.

  7. The event handler code is executed, and the "Hello World!" message is displayed.

  8. Click OK to return to the Microsoft Visual Studio Tools for Applications code editor, and then click Stop Debugging on the Debug menu (or press Ctrl+Alt+Break).

Getting the Current User's Name

In the following example, you will learn how to use the UserName property of the User class to retrieve the name of the current user and populate the value of a Text Box control by using an event handler for the Loading event.

Populating the Text Box control is accomplished by using an instance of the XPathNavigator class to write the current user's name to the XML node that the control is bound to.

First, the MainDataSource property of the XmlForm class is called to retrieve an instance of the DataSource class that represents the underlying XML document of the form. The DataSource object then calls the CreateNavigator method, which creates the XPathNavigator object and positions it at the root node of the form's main data source.

The SelectSingleNode method of the XPathNavigator class is called to select the employee field in the form's data source. Finally, the SetValue method is called to set the value of the field with the UserName property.

For more information on working with System.Xml in managed code form templates, see How to: Work with the XPathNavigator and XPathNodeIterator Classes.

Add a Loading event handler

  1. Open the HelloWorld form template that you created in the previous walkthrough in the InfoPath designer.

  2. On the View tab, select Show Fields.

  3. Right click the myFields folder, and then click Add.

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

  5. Drag the employee field onto the view.

  6. On the Developer tab, click Loading Event.

    This will create an event handler for the Loading event, and move the focus to that event handler in the code editor.

  7. In the code editor, type the following:

    public void FormEvents_Loading(object sender, LoadingEventArgs e)
    {
       XPathNavigator dataSource;
       dataSource = this.MainDataSource.CreateNavigator();
       dataSource.SelectSingleNode(
          "/my:myFields/my:employee", NamespaceManager).SetValue(this.User.UserName);
    }
    
    Public Sub FormEvents_Loading(ByVal sender As Object, ByVal e As LoadingEventArgs)
       Dim dataSource As XPathNavigator
       dataSource = Me.MainDataSource.CreateNavigator
       dataSource.SelectSingleNode( _
          "/my:myFields/my:employee", NamespaceManager).SetValue(Me.User.UserName)
    End Sub
    
  8. Switch to the InfoPath form design window, and then click the Preview button on the Home tab to preview the form.

    The employee field should automatically fill in with your user name.

Next Steps

See Also

Reference

XmlForm