Konten ini tidak tersedia dalam bahasa Anda dan berikut adalah versi bahasa Inggris.

FormEvents.ContextChanged event

Office 2013 and later

Occurs after the context node changes.

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

public abstract event ContextChangedEventHandler ContextChanged


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

Important noteImportant

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 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.

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", 

      // Set DisplayContext with the name of the current context.