OnContextChange Event

Occurs after the context node changes.

Function XDocument::OnContextChange(ByRef pEvent As DocContextChangeEvent)

pEvent Required DocContextChangeEvent. A reference to the DocContextChangeEvent object.

Remarks

The context node is the XML DOM 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 DOM for both items mapped to the view.

The OnContextChange 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 document loads, or when a view change occurs, the OnContextChange event will occur after the OnLoad and OnSwitchView events occur.

When the IsUndoRedo property of the DocContextChangeEvent 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 OnContextChange event handler that modify the XML DOM 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 OnContextChange 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.

Note  This object model member is not supported when the Disable Service Pack features option on the Advanced tab of the Options dialog box in InfoPath is selected or when Microsoft Office 2003 Service Pack 1 or later is not installed. Any form that implements this object model member in its code will generate an error message if it is opened in InfoPath when service pack features are disabled or unavailable.

Example

In the following example, instructions specific to different context nodes in a form are added to the body of a custom task pane. A custom HTML task pane should be added to the form template in design mode:

function XDocument::OnContextChange(eventObj) 
{
    var oContextNode = eventObj.Context;

    var strText = "";
    if( oContextNode.nodeName == "my:root" )
        strText = "";
    else if( oContextNode.nodeName == "my:singleName" )
        strText = "Type your full name.";
    else if( oContextNode.nodeName == "my:webSite" )
        strText = "Type the Web address of your personal web page.";

    var oTaskPane = XDocument.View.Window.TaskPanes.Item(0);
    oTaskPane.HTMLDocument.body.innerText = strText;
}

Applies to | XDocument Object