The XDocument.View.SelectNodes() function can be very useful when you need to "jump" and give focus to a certain field on your form with code. However, the SelectNodes() function only allows for the selection of certain controls and there are several common controls that are not normally selectable. Drop-down and normal lists, check boxes, radio buttons, and ink picture controls cannot be selected directly in InfoPath. To get around this problem we will place the non-selectable controls within a section control that is bound to the same node as our target control. Once this is accomplished we can use the SelectNodes() function on the containing section to achieve the desired effects.
In this task we will create a drop-down list and a button that will change focus to this drop-down list. Let's start by creating a new blank form.
Add a textbox, drop-down list, and button control:
- Open the Controls task pane.
- Insert a Text Box into the view.
- Press Enter twice to insert some white space.
- Insert a Drop-Down List Box into the view.
- Press Enter twice to insert some white space.
- Insert a Button into the view.
Rename the Drop-Down List Box schema node and Button:
- Double-click the Button in the view, change the Label to Change Focus, and then change the ID to btnChangeFocus, as shown in Figure 1, and then click OK.
- Open the Data Source task pane, and then choose Main from the Data Source drop down list.
- Double-click myFields/field2, and then rename it to DropDown.
- Double-click myFields/field1, and then rename it to Date.

Figure 1. The completed default view.
Add code to the button:
- Double-click on the button control in the view.
- Click Edit Form Code.
- In the Microsoft Script Editor, replace the contents of the OnClick event handler with the following code:
BLOCKED SCRIPT XDocument.View.SelectNodes(XDocument.DOM.selectSingleNode("//my:DropDown"));
C#: thisXDocument.View.SelectNodes(thisXDocument.DOM.selectSingleNode("//my:DropDown"), Type.Missing, Type.Missing); |
After reading the documentation on the SelectNodes() function it might seem that our form would work correctly as it has been made so far, but a short preview will prove otherwise.
Preview the Error:
- Click Preview Form on the Standard Toolbar.
- Click Change Focus (an error message pops up, as shown in Figure 2).
- Click No, click OK, and then click Close Preview.

Figure 2. The SelectNodes() error message.
Now we must add a section that is bound to the same node as our drop-down list box. The reason we bind it to the same node and not create a new node is to prevent excessive clutter in the DOM.
Add a section and move the drop-down list:
- Open the Data Source task pane.
- Choose Main from the Data Source drop down list.
- Right-drag the DropDown node onto the view, and then choose Section.
- Drag-and-drop the drop-down list box into the section that was just created.
- Rearrange and resize the new section to match Figure 3.

Figure 3. The section and drop-down list bound to the same node.
At this point it might still seem like the button will work correctly, but another preview will show us that this is not the case. The section that contains the drop-down list must be set to allow users to delete the section in order for the button to work.
Make the section able to be deleted:
- Double-click the section.
- Select the Allow Users To Delete The Section check box under the Default Settings section.
- Click OK.
Finally, a preview after these steps shows us that we have accomplished our goal. Previewing the form and clicking the button makes focus "jump" from the text box to the section that holds the drop-down list without issuing any errors. Once focus is set to the section, the user must simply press any of the arrow keys or the tab key to choose a new value for the drop down list.
In the last few steps we enabled the ability to delete the section, but this should not be permissible. To avoid this problem we use a clever conditional formatting technique to ensure that the section will never be deleted.
Add conditional formatting:
- Double-click the section.
- Click the Display tab.
- Click Conditional Formatting.
- Click Add.
- Choose The Expression from the first drop-down list.
- Replace the text in the text-box to the right of the drop-down list with 2+2=4 (or any other tautology: a statement that is always true).
- Select the Don't Allow Users To Insert Or Delete This Control check box.
- Click OK three times.
After these steps the button will work exactly as it is supposed to and there are no chances of the section being removed or deleted from the form. All of these steps can be applied to the other non-selectable items (check boxes and option buttons) to achieve the same effect.