How to: Add an Event Handler

The procedures for adding event handlers to an Microsoft Office InfoPath 2007 managed code form template using Microsoft Visual Studio Tools for Applications (VSTA) are similar to those used when adding event handlers for script. For example, in order to add an event handler for the Loading event, open the form template in InfoPath design mode, point to Programming on the Tools menu, and then click Loading Event. The focus automatically switches to the skeleton event handler in the VSTA code editor.

The same procedures are used from Visual Studio 2005 with Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System, or from Visual Studio 2008 with Visual Studio Tools for Office with the exception of adding event handlers for form-level events (the Loading, ViewSwitched, ContextChanged, and Sign events). These differences are called out in the following procedures.

Important

Adding an event handler generates event binding code in the he InternalStartup method of the FormCode.cs or FormCode.vb file in your form template project. You should not create the InternalStartup method or add any additional code within it yourself.

Add an event handler for the Click event of a Button control

  1. If you are working in InfoPath, open the form template in design mode.

    If you are working in Visual Studio, open an InfoPath Form Template project.

  2. On the Controls task pane or the Visual Studio Toolbox, drag a Button to the form.

  3. Double-click the button, and then click Edit Form Code.

    The focus switches to the skeleton event handler for the Clicked event in the VSTA or Visual Studio code editor.

Add an event handler for the Changing, Validating, or Changed event of a field or group

  1. If you are working in InfoPath, open the form template in design mode.

    If you are working in Visual Studio, open an InfoPath Form Template project.

  2. Right-click a data-entry control bound to the field or group, such as a Text Box control.

  3. Point to Programming, and then click the event you want to create an event handler for. The focus switches to the skeleton event handler for the Changing, Validating, or Changed event in the VSTA or Visual Studio code editor.

    Note

    If you selected the Enable browser-compatible features only check box in the Design a Form dialog box when you created the form template, or if you selected the Design a form template for both InfoPath and Forms Services check box in the Form Options dialog box in the Compatibility category, the command for the Changing Event will not be available. This is because event handlers for the Changing event are not supported in the business logic of form templates published to document libraries on Microsoft Office Forms Server 2007 or Microsoft Office SharePoint Server 2007 with InfoPath Forms Services.

Add an event handler for the Loading, ViewSwitched, ContextChanged, and Sign events of a form

  1. If you are working in InfoPath, open the form template in design mode.

    If you are working in Visual Studio, open an InfoPath Form Template project.

  2. In InfoPath, point to Programming on the Tools menu, and then click the form event that you want to write an event handler for.

    In Visual Studio, point to Event on the Insert menu, and then click the form event you want to write an event handler for.

    The focus switches to the skeleton event handler for the Loading, ViewSwitched, ContextChanged, or Sign event in the VSTA or Visual Studio code editor.

    Note

    If you selected the Enable browser-compatible features only check box in the Design a Form dialog box when you created the form template, or if you selected the Design a form template for both InfoPath and Forms Services check box in the Form Options dialog box in the Compatibility category, the command for the Context Changed Event or the Sign Event will not be available. This is because event handlers for the ContextChanged and Sign events are not supported in the business logic of form templates published to document libraries on Microsoft Office Forms Server 2007 or Microsoft Office SharePoint Server 2007 with InfoPath Forms Services.

Add an event handler for the Submit event of a form

  1. If you are working in InfoPath, open the form template in design mode.

    If you are working in Visual Studio, open an InfoPath Form Template project, and then click the manifest.xsf [Design] tab to display the form designer.

  2. On the Tools menu, click Form Submit Options.

  3. Click Allow users to submit this form, click Perform custom action using Code, and then click Edit Code.

    The focus switches to the skeleton event handler for the Submit event in the VSTA or Visual Studio code editor.

Add an event handler for the Save event of a form

  1. If you are working in InfoPath, open the form template in design mode.

    If you are working in Visual Studio, open an InfoPath Form Template project, and then click the manifest.xsf [Design] tab to display the form designer.

  2. On the Tools menu, click Form Options.

  3. Click the Open and Save category, select the Save using custom code check box, and then click Edit.

    The focus switches to the skeleton event handler for the Save event in the VSTA or Visual Studio code editor.

    Note

    If you selected the Enable browser-compatible features only check box in the Design a Form dialog box when you created the form template, or if you selected the Design a form template for both InfoPath and Forms Services check box in the Form Options dialog box in the Compatibility category, the Save using custom code check box will not be available. This is because event handlers for the Save event are not supported in the business logic of form templates published to document libraries on Microsoft Office Forms Server 2007 or Microsoft Office SharePoint Server 2007 with InfoPath Forms Services.

Add an event handler for the VersionUpgrade event of a form

  1. If you are working in InfoPath, open the form template in design mode.

    If you are working in Visual Studio, open an InfoPath Form Template project, and then click the manifest.xsf [Design] tab to display the form designer.

  2. On the Tools menu, click Form Options.

  3. Click the Programming category, select Use custom event in the On version upgrade drop-down box, and then click Edit.

    The focus switches to the skeleton event handler for the Save event in the VSTA or Visual Studio code editor.

Add an event handler for the Merge event of a form

  1. If you are working in InfoPath, open the form template in design mode.

    If you are working in Visual Studio, open an InfoPath Form Template project, and then click the manifest.xsf [Design] tab to display the form designer.

  2. On the Tools menu, click Form Options.

  3. Click the Advanced category, click the Enable form merging check box, and then click Edit.

    The focus switches to the skeleton event handler for the Merge event in the VSTA or Visual Studio code editor.

    Note

    If you selected the Enable browser-compatible features only check box in the Design a Form dialog box when you created the form template, or if you selected the Design a form template for both InfoPath and Forms Services check box in the Form Options dialog box in the Compatibility category, the Enable form merging will not be available. This is because event handlers for the Merge event are not supported in the business logic of form templates published to document libraries on Microsoft Office Forms Server 2007 or Microsoft Office SharePoint Server 2007 with InfoPath Forms Services.

See Also

Tasks

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