How to: Add an Event Handler

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

This topic describes the procedures for adding event handlers to an Microsoft InfoPath 2010 managed code form template using Microsoft Visual Studio Tools for Applications. To add an event handler to a form template, you start with the form template open in the InfoPath designer, and then select the appropriate user interface command for the event you want to write code for. After you select the command for an event in the InfoPath designer, the focus automatically switches to the skeleton event handler for that event in the Microsoft Visual Studio Tools for Applications code editor.

Important

You should always use the InfoPath designer user interface to add an event handler. Adding an event handler with the user interface generates event binding code in the 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. Open the form template in the InfoPath designer, and then add a Button control to the form.

  2. Click the button, and then on the Properties tab of the ribbon, click Custom Code.

    The focus switches to the skeleton event handler for the Clicked event in the Microsoft Visual Studio Tools for Applications code editor.

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

  1. Open the form template in the InfoPath designer.

  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 Microsoft Visual Studio Tools for Applications code editor.

    Note

    The command to create an event handler for the Changing event is not available if the compatibility setting for the form template is set to Web Browser Form. This is because the Changing event is not supported in the business logic of form templates published to document libraries on Microsoft SharePoint Server 2010 with InfoPath Forms Services. To create an event handler for the Changing event, you must change the compatibility setting to InfoPath Editor in the InfoPath designer. To do that, click the File tab, click Form Options, click Compatibility, and then set Form type to InfoPath Editor Form.

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

  1. Open the form template in the InfoPath designer.

  2. On the Developer tab of the ribbon, click the form event that 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 Microsoft Visual Studio Tools for Applications code editor.

    Note

    The commands to create an event handler for the ContextChanged or Sign events are not available if the compatibility setting for the form template is set to Web Browser Form. This is because those events are not supported in the business logic of form templates published to document libraries on Microsoft SharePoint Server 2010 with InfoPath Forms Services. To create an event handler for the ContextChanged or Sign event, you must change the compatibility setting to InfoPath Editor Form in the InfoPath designer. To do that, click the File tab, click Form Options, click Compatibility, and then set Form type to InfoPath Editor Form.

Add an event handler for the Submit event of a form

  1. Open the form template in the InfoPath designer.

  2. Click the File tab, click Submit To on the Info tab, and then click 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 Microsoft Visual Studio Tools for Applications code editor.

Add an event handler for the Save event of a form

  1. Open the form template in the InfoPath designer.

  2. Click the File tab, and then click Form Options on the Info tab.

  3. Click the 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 Microsoft Visual Studio Tools for Applications code editor.

    Note

    The Save using custom code check box is not available if the compatibility setting for the form template is set to InfoPath Forms Services. This is because the Save event is not supported in the business logic of form templates published to document libraries on Microsoft SharePoint Server 2010 with InfoPath Forms Services. To create an event handler for the Save event, you must change the compatibility setting to InfoPath Editor Form in the InfoPath designer. To do that, click the File tab, click Form Options, click Compatibility, and then set Form type to InfoPath Editor Form.

Add an event handler for the VersionUpgrade event of a form

  1. Open the form template in the InfoPath designer.

  2. Click the File tab, and then click Form Options on the Info tab.

  3. Click the Versioning category, select Use custom event in the Update existing forms drop-down box, and then click Edit.

    The focus switches to the skeleton event handler for the Save event in the Microsoft Visual Studio Tools for Applications code editor.

Add an event handler for the Merge event of a form

  1. Open the form template in the InfoPath designer.

  2. Click the File tab, and then click Form Options on the Info tab.

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

    The focus switches to the skeleton event handler for the Merge event in the Microsoft Visual Studio Tools for Applications code editor.

    Note

    The Enable form merging check box is not available if the compatibility setting for the form template is set to InfoPath Forms Services. This is because the Merge event is not supported in the business logic of form templates published to document libraries on Microsoft SharePoint Server 2010 with InfoPath Forms Services. To create an event handler for the Merge event, you must change the compatibility setting to InfoPath Editor Form in the InfoPath designer. To do that, click the File tab, click Form Options, click Compatibility, and then set Form type to InfoPath Editor Form.

See Also

Tasks

Walkthrough: Creating a Basic Form Template with Code