Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

FormEvents.Save Event

Occurs when the Save or Save As commands are used from the user interface, or when the Save and SaveAs methods are used.

Namespace:  Microsoft.Office.InfoPath
Assembly:  Microsoft.Office.InfoPath (in Microsoft.Office.InfoPath.dll)

public abstract event SaveEventHandler Save

ExceptionCondition
InvalidOperationException

The developer attempted to bind the event in some location other than the InternalStartup method.

Important noteImportant

The Save event is not meant to be instantiated by the developer in form code. You should only add event handlers for form-level events from the Microsoft InfoPath 2010 design mode user interface. When you add an event handler to your form template from the design mode user interface, InfoPath generates code in the InternalStartup method of your form code file using the EventManager class and the member of the FormEvents class to bind the event to its event handler. For information on how to add event handlers in InfoPath design mode, see How to: Add an Event Handler.

The Save event is raised only if the form template has the Save using custom code option set in the Form Options dialog box.

The Save event is bound using the SaveEventHandler delegate.

The Save event can be cancelled by using the CancelableArgs property of the SaveEventArgs class to set the Cancel property to true.

The SaveEventArgs object, which is passed as a parameter to an event handler for the Save event, provides properties and methods that can be used to get the form's file name, determine save status, and perform the save operation.

This type or member can be accessed only from code running in forms opened in Microsoft InfoPath Filler.

In the following example, the event handler for the Save event checks to see if Field2 is empty, and if it is not performs a save operation. If it is empty, it displays a message and cancels the save operation.

public void FormEvents_Save(object sender, SaveEventArgs e)
{
   // Check to see if Field2 is empty.
   XPathNavigator reqField = 
      CreateNavigator().SelectSingleNode("/my:myFields/my:field2", 
      NamespaceManager);
   if(reqField.ToString() == "")
   {
      MessageBox.Show("Field2 is empty.\nYou cannot save the form.");
      e.CancelableArg.Cancel = true;
   }
   else
   {
   // The Dirty property will be set to false if save is successful.
      e.PerformSaveOperation();
      e.CancelableArgs.Cancel = false;
   }
}
Show:
© 2015 Microsoft