Programmatic Selection in InfoPath

InfoPath supports two methods on the View OM object - "SelectText" for data entry controls and "SelectNodes" for structural controls - in order to enable programmatic selection.

Data entry controls that can be programmatically selected (using View.SelectText):

  • Text Box
  • Rich Text Box
  • Date Picker

Structural controls that can be programmatically selected (using View.SelectNodes):

  • Optional Section
  • Choice Section
  • Repeating Section (items)
  • Repeating Table (rows)
  • Repeating Recursive Section (items)
  • Bulleted, Numbered & Plain List
  • File Attachment

Programmatically setting focus to these controls is not supported:

  • Drop-Down List Box
  • List Box
  • Check Box
  • Option Button
  • Button
  • Picture (Linked or Included)
  • Ink Picture
  • Hyperlink
  • Expression Box / Vertical Label
  • ActiveX
  • Section

Examples:

Steps to select rows in a repeating table using SelectNodes:

In order to set selection extending the 1st two rows of a repeating table you can do the following steps:

1. Insert a "repeating table" from the controls task pane

2. Insert a "button" from the controls task pane

3. Double click on the button control to bring up Button Properties

4. Click on the "Edit Form Code" button

[you may be asked to install Microsoft Script Editor if you don't already have it installed]

5. This should bring up script.js file in Microsoft Script Editor with a function such as

function

CTRL5_5::OnClick(eventObj)

{

// Write your code here

}

6. Insert the following code inside the above function

var nodeSelStart = XDocument.DOM.selectSingleNode("/my:myFields/my:group1/my:group2[1]")

var nodeSelEnd = XDocument.DOM.selectSingleNode("/my:myFields/my:group1/my:group2[2]")

XDocument.View.SelectNodes(nodeSelStart, nodeSelEnd)

[This assumes that the repeating table is bound to my:group2. You can figure the XPath by looking at the Data Source task pane after selecting the appropriate repeating table.]

7. Now you should be able to preview the form and verify that clicking on the button causes the expected selection. Please note that you have to have at least two rows in the table in order to select the first two rows. Otherwise you will see an error.

Steps to select text in a text box using SelectText:

In order to select text in a text box you can do the following steps:

1. Insert a "text box" from the controls task pane

2. Insert a "button" from the controls task pane

3. Double click on the button control to bring up Button Properties

4. Click on the "Edit Form Code" button

[you may be asked to install Microsoft Script Editor if you don't already have it installed]

5. This should bring up script.js file in Microsoft Script Editor with a function such as

function

CTRL2_5::OnClick(eventObj)

{

// Write your code here

}

6. Insert the following code inside the above function

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

XDocument.View.SelectText(nodeSelText)

[This assumes that the text box is bound to my:field1. You can figure the XPath by looking at the Data Source task pane after selecting the appropriate text box.]

7. Now you should be able to preview the form and verify that clicking on the button causes the expected selection.