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
- Anonymous
November 17, 2004
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... - Anonymous
April 08, 2005
In a previous blog entry we showed how to add buttons to move rows in a Repeating Table up and down.... - Anonymous
April 21, 2009
PingBack from http://vingrad.ru/blogs/cookish/2009/04/18/pozitsionirovanie-fokusa-na-nuzhnyiy-kontrol-v-infopath/ - Anonymous
March 11, 2010
Hi,Optional Section just bind to a single field, not a group. How can I indicate "nodeSelEnd" in "XDocument.View.SelectNodes(nodeSelStart, nodeSelEnd)" method? - Anonymous
August 05, 2010
How to select a textbox which is in view2 for example and u r clicking the button which is in view1. - Anonymous
August 22, 2013
How can we do the same in InfoPath 2007 - Anonymous
November 29, 2013
<a href="eyeofthedemon.ucoz.com/.../1-1-0-48">Chess (240x320)</a> <a href="freelancehunt.com/.../a> - Anonymous
December 25, 2013
http://advego.ru/5MQystWpUN Общайся на форумах и получай деньги - Advego.ru - Anonymous
January 16, 2014
Хорошо.http://olis.in.ua/http://eyeofthedemon.ucoz.com/ - Anonymous
May 21, 2014
Comment<a href="supercelkerplus.mybb3.ru/" target="_blank">тут</a> - Anonymous
July 26, 2015
Описание: Модный дайджест - строительной информации, статьи и новости на тематику по улутшению дома. Ключевые слова: блог, домашний, уют, огнезащита, гидроизоляция, бытовая, техника, оборудование, производство, бетон, железобетон, панели, водоснабжение, сантехника, бассейны, ворота, заборы, ограждения. Сайт: http://paralleli.do.am/