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.


Comments (12)

  1. Anonymous says:

    Working with InfoPath often requires you to unlearn some things you probably take for granted if you’ve been developing windows or web apps for a while. On my current project, each InfoPath form has two views which show either the…

  2. Anonymous says:

    In a previous blog entry we showed how to add buttons to move rows in a Repeating Table up and down….

  3. mazong1123 says:

    Hi,

    Optional Section just bind to a single field, not a group. How can I indicate "nodeSelEnd" in "XDocument.View.SelectNodes(nodeSelStart, nodeSelEnd)" method?

  4. Anonymous says:

    How to select a textbox which is in view2 for example and u r clicking the button which is in view1.

  5. Anonymous says:

    How can we do the same in InfoPath 2007

  6. Anonymous says:

    http://advego.ru/5MQystWpUN Общайся на форумах и получай деньги – Advego.ru

  7. Anonymous says:

    Comment

    <a href="supercelkerplus.mybb3.ru/" target="_blank">тут</a>

  8. Блог - параллели домашнего уюта. says:

    Описание: Модный дайджест – строительной информации, статьи и новости на тематику по улутшению дома.

    Ключевые слова: блог, домашний, уют, огнезащита, гидроизоляция, бытовая, техника, оборудование, производство, бетон, железобетон, панели, водоснабжение, сантехника, бассейны, ворота, заборы, ограждения.

    Сайт: http://paralleli.do.am/