FormEvents.ContextChanged Event

Occurs after the context node changes.

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

Syntax

'Declaration
Public MustOverride Event ContextChanged As ContextChangedEventHandler
'Usage
Dim instance As FormEvents
Dim handler As ContextChangedEventHandler

AddHandler instance.ContextChanged, handler
public abstract event ContextChangedEventHandler ContextChanged

Exceptions

Exception Condition
InvalidOperationException

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

Remarks

Important

The ContextChanged 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 ContextChanged event is bound using the ContextChangedEventHandler delegate.

The context node is the XML node mapped to the view that corresponds to the container (or item) with the current XML selection. For example, if the current selection in the view is in a text box, the context node is the node to which the text box is bound. If the current selection is a repeating section, the context node is the node for that item. If two repeating sections are selected, the context node is the ancestor XML node for both items mapped to the view.

The ContextChanged event is asynchronous. It does not fire on every change in the context node; instead, it fires after the application has stopped processing other events.

When the underlying XML document loads, or when a view change occurs, the ContextChanged event will occur after the Loading and ViewSwitched events occur.

When the UndoRedo property of the ContextChangedEventArgs object is true, the context change was caused by a user's undo or redo operation rather than an explicit user context change. Operations performed within the ContextChanged event that modify the underlying XML document should be avoided in response to undo or redo operations, because they may interfere with the user's intention to revert data to a previous state.

For Rich Text Box controls, the ContextChanged event is not raised for context changes within the XHTML content, that is, selection changes to the rich text in the control. The GetContextNodes method can be used to determine the selection within Rich Text Box controls.

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

Examples

The following example assumes that your form has a Text Box control bound to a field named DisplayContext, as well as controls bound to other fields and groups on the form. Moving the selection to different fields and groups displays the name of the field or group in the Text Box bound to DisplayContext.

public void FormEvents_ContextChanged(object sender, 
   ContextChangedEventArgs e)
{
   if (e.ChangeType == "ContextNode")
   {
      // Position a XPathNavigator on the DisplayContext field.
      XPathNavigator root, txtbox;
      root = this.MainDataSource.CreateNavigator();
      txtbox = root.SelectSingleNode("/my:myFields/my:DisplayContext", 
         this.NamespaceManager);

      // Set DisplayContext with the name of the current context.
      txtbox.SetValue(e.Context.Name);
      return;
   }
}
Public Sub FormEvents_ContextChanged(ByVal sender As Object, _
   ByVal e As ContextChangedEventArgs)
   If (e.ChangeType = "ContextNode") Then
      ' Position a XPathNavigator on the DisplayContext field.
      Dim root, txtbox As XPathNavigator
      root = Me.MainDataSource.CreateNavigator
      txtbox = root.SelectSingleNode("/my:myFields/my:DisplayContext", 
         Me.NamespaceManager)

      ' Set DisplayContext with the name of the current context.
      txtbox.SetValue(e.Context.Name)
      Return
   End If
End Sub

See Also

Reference

FormEvents Class

FormEvents Members

Microsoft.Office.InfoPath Namespace