Share via


Walkthrough: Creating a Basic Managed Code Form Template

When using Microsoft Office InfoPath 2003 to write business logic in a managed code language (Visual Basic or C#), you use an InfoPath project created with Microsoft Visual Studio .NET 2003 and the Microsoft Office InfoPath 2003 Toolkit for Visual Studio .NET or Visual Studio 2005 Tools for the Microsoft Office System with the Microsoft Office InfoPath 2003 Toolkit for Visual Studio 2005. Both of these managed code development environments depend on and support only the COM Interop managed code object model provided by the Microsoft.Office.Interop.InfoPath.SemiTrust assembly

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

Alternatively, if you have 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 installed, you can start from Visual Studio, create an InfoPath form template project, and then use the InfoPath design mode integration to design your form and add business logic all within the Visual Studio environment.

The first walkthrough shows you how to create a simple Hello World application using C# or Visual Basic in the VSTA and Visual Studio development environments. The walkthrough concludes with a code sample that shows you how to use the System.Environment.UserName property 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 VSTA development environment, you will need:

  • Microsoft Office InfoPath 2007 with Microsoft Visual Studio Tools for Applications (VSTA) installed.

In order to complete this walkthrough using the Visual Studio development environment with InfoPath design mode integration, you will need:

  • Microsoft Office InfoPath 2007

  • Microsoft 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

Note

For information on how to download and install Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System, see the InfoPath Developer Portal.

Hello World in VSTA

In the following walkthrough, you will learn how 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. These procedures assume you are using the Microsoft Visual Studio Tools for Applications (VSTA) development environment.

Create a new project and specify the programming language (Visual Studio Tools for Applications)

  1. Start InfoPath.

  2. In the Fill Out a Form dialog box, click Design a Form Template under Design a Form.

    Note

    If InfoPath is already running, to display the Design a Form dialog box, click Design a Form on the File menu.

  3. In the Design a Form Template dialog box, click Form Template, click Blank, clear the Enable browser-compatible features only check box, and then click OK.

  4. On the Tools menu, click Form Options.

  5. In the Category list, click Programming, select either Visual Basic or C# from the Form template code language drop-down list, and then click OK.

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

Add a Button control and event handler

  1. In Design Tasks task pane, click Controls.

  2. Drag a Button control onto the form.

  3. Double-click the Button control, type Hello for the Label property, and then click Edit Form 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 design mode window.

  3. Click the Preview button on the Standard toolbar.

  4. Click the Hello 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. Switch back to the VSTA 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.

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

  6. On the Debug menu, click Step Over (or press F10) 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 VSTA code editor, and then click Stop Debugging on the Debug menu (or press Ctrl+Alt+Break).

Hello World in Visual Studio with InfoPath Design Mode Integration

The following walkthrough shows how to write the same Hello World example as the previous walkthrough, but instead using Visual Studio 2005 with the InfoPath design mode integration enabled by installing Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System, or Visual Studio 2008 with Visual Studio Tools for Office.

Create a new project and specify the programming language (Visual Studio)

  1. Start Visual Studio.

  2. On the File menu, point to New, and then click Project.

  3. Under Project types, expand the Visual C# folder, click Office (in Visual Studio 2008, then click 2007), and then click InfoPath Form Template.

    Alternatively, you can expand the Other Languages folder, expand the Visual Basic folder, click Office (in Visual Studio 2008, then click 2007), and then click InfoPath Form Template.

  4. Name the project HelloWorld, and then click OK.

  5. In the Design a Form dialog box, click Form Template, click Blank, and then click OK.

    The integrated InfoPath form template designer is displayed on a tab titled manifest.xsf [Design].

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

Add a Button control and event handler

  • In Design Tasks task pane, click Controls.

Note

If the Design Tasks task pane is not visible, click Design Tasks on the View menu.

  1. Drag a Button control from the Toolbox onto the form.

  2. Double-click the Button control, type Hello for the Label property, and then click Edit Form Code. When prompted, save the form.

    This will open the code editor window 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 hander 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. On the Debug menu, click Start Debugging (or press F5).

    The InfoPath Preview window is displayed.

  3. Click the Hello button.

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

  4. Click OK, and then click the Close Preview button on the Standard toolbar to return to Visual Studio.

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

Debug form code

  1. Click the FormCode.cs or FormCode.vb tab to return to the code editor.

  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.

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

  6. On the Debug menu, click Step Over (or press F10) 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 code editor, and then click Stop Debugging on the Debug menu (or press Ctrl+Alt+Break).

Getting the Current User's Name

By using the .NET Framework classes, you can get access to additional functionality that is not easily available when working with business logic written in script. In the following example, you will learn how to use the UserName property of the Environment class to retrieve the name of the current user and populate the value of a Text Box control using an event handler for the Loading event.

Populating the Text Box control is accomplished by using two instances of the XPathNavigator class.

The first instance uses the MainDataSource property of the XmlForm class to retrieve an instance of the DataSource class representing the underlying XML document of the form. This instance of the DataSource class then uses the CreateNavigator method to position the XPathNavigator at the root node of XML document.

The second instance of the XPathNavigator is positioned to the employee field in the XML document by using the first instance, and then the SetValue method of the XPathNavigator class is used 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 project that you created in the previous walkthrough.

    If you are working in InfoPath with Microsoft Visual Studio Tools for Applications (VSTA), start InfoPath and open the form in design mode in InfoPath. If you are working with 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, start Visual Studio and open the project.

  2. On the View menu, click Data Source.

  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. If you are working in InfoPath, on the Tools menu, click Programming, and then click Loading Event.

    If you are working in Visual Studio, on the Insert menu, click Event, and then 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)
    {
       System.Xml.XPath.XPathNavigator root, user;
       root = this.MainDataSource.CreateNavigator();
       user = root.SelectSingleNode("//my:employee", this.NamespaceManager);
       user.SetValue(System.Environment.UserName);
    }
    
    Public Sub FormEvents_Loading(ByVal sender As Object, ByVal e As LoadingEventArgs)
       Dim root, user As System.Xml.XPath.XPathNavigator
       root = Me.MainDataSource.CreateNavigator
       user = root.SelectSingleNode("/my:myFields/my:employee", Me.NamespaceManager)
       user.SetValue(System.Environment.UserName)
    End Sub
    
  8. If you are working in Microsoft Visual Studio Tools for Applications (VSTA), switch to the InfoPath form design window, and then click the Preview button to preview the form.

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

Next Steps

See Also

Reference

XmlForm