Share via


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)

Syntax

'Declaration
Public MustOverride Event Save As SaveEventHandler
'Usage
Dim instance As FormEvents
Dim handler As SaveEventHandler

AddHandler instance.Save, handler
public abstract event SaveEventHandler Save

Exceptions

Exception Condition
InvalidOperationException

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

Remarks

Important

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.

Examples

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;
   }
}
Public Sub FormEvents_Save(ByVal sender As Object, _
   ByVal e As SaveEventArgs)
   ' Check to see if Field2 is empty.
   Dim reqField as XPathNavigator = 
      CreateNavigator().SelectSingleNode("/my:myFields/my:field2", _
      NamespaceManager)
   If(reqField.ToString() = "") Then
      MessageBox.Show("Field2 is empty" & vbNewLine & _
         "You cannot save the form.")
      e.CancelableArgs.Cancel = True
   Else
   ' The Dirty property will be set to false if save is successful.
   e.PerformSaveOperation()
   e.CancelableArgs.Cancel = False
   End If
End Sub

See Also

Reference

FormEvents Class

FormEvents Members

Microsoft.Office.InfoPath Namespace