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):
Structural controls that can be programmatically selected (using View.SelectNodes):
Programmatically setting focus to these controls is not supported:
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
{
// 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
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.
PingBack from http://vingrad.ru/blogs/cookish/2009/04/18/pozitsionirovanie-fokusa-na-nuzhnyiy-kontrol-v-infopath/
Hi,
Optional Section just bind to a single field, not a group. How can I indicate "nodeSelEnd" in "XDocument.View.SelectNodes(nodeSelStart, nodeSelEnd)" method?
How to select a textbox which is in view2 for example and u r clicking the button which is in view1.