This documentation is archived and is not being maintained.

Programmatically Selecting Controls in an InfoPath View

Office 2007

Microsoft Office InfoPath 2007 provides two methods of the View object to programmatically select a control in the current view, namely: the SelectText and SelectNodes methods. The SelectText method is used for data entry controls, such as a Text Box, while the SelectNodes method is used for structural controls, such as an Optional Section. To select a particular control in the view, you need to provide the node and, optionally, the control's ViewContext ID. The ViewContext ID is needed when you have multiple controls bound to the same node in the data source. Office InfoPath 2007 provides the ViewContext ID information in design mode.

Finding the Context ID

A control's ViewContext ID is listed on the Advanced tab of the control's properties dialog box, which is accessed in the following ways:

  • Select the control and choose the control's properties from the Format menu. For instance, if you have a Text Box control selected, choose Text Box Properties on the Format menu.
  • Right-click the control and click Text Box Properties .
  • Select the control and press Alt+Enter.

The ViewContext ID of a control is listed in the Code section of the Advanced tab of the control's properties dialog box.

When to use SelectText and SelectNodes

Data entry controls that can be programmatically selected using the SelectText method:

  • Text Box
  • Rich Text Box
  • Date Picker

Structural controls that can be programmatically selected using the SelectNodes method:

  • Optional Section
  • Choice Section
  • Repeating Section (items)
  • Repeating Table (rows)
  • Repeating Recursive Section (items)
  • Bulleted, Numbered, and Plain List
  • Horizontal Repeating Table

You cannot programmatically select, or set focus to, the following controls:

  • Drop-Down List Box
  • List Box
  • Check Box
  • Option Button
  • Button
  • Picture (linked or included)
  • Ink Picture
  • Hyperlink
  • Expression Box
  • Vertical Label
  • ActiveX Section
  • Horizontal Region

Using SelectText and SelectNodes

In the following example, the SelectText method is used to select a Text Box that is bound to "my:field1".

var nodeSelText = XDocument.DOM.selectSingleNode("/my:myFields/my:field1")
XDocument.View.SelectText(nodeSelText)

If you had multiple controls bound to "my:field1", you would use the optional parameter varViewContext to select a specific control. In the following example, there are two Text Box controls bound to "my:field1", with the first one having a ViewContext ID of "CTRL1" and the second one having a ViewContext ID of "CTRL8". The second control is selected:

var nodeSelText = XDocument.DOM.selectSingleNode("/my:myFields/my:field1")
XDocument.View.SelectText(nodeSelText, "CTRL8")

In the following example, the SelectNodes method is used to select the first row in a repeating table bound to the repeating group "my:employee".

var objXMLNode = Xocument.DOM.selectSingleNode("//my:employees/my:employee");
XDocument.View.SelectNodes(objXMLNode);

You can also select multiple rows in a repeating table. In the following example, the first two rows of a repeating table bound to the repeating group "my:employee" are selected using the SelectNodes method:

var nodeSelStart = XDocument.DOM.selectSingleNode("//my:employees/my:employee[1]")
var nodeSelEnd = XDocument.DOM.selectSingleNode("//my:employees/my:employee[2]")
XDocument.View.SelectNodes(nodeSelStart, nodeSelEnd)


Show: