«

Setting Focus In InfoPath

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 full data set, or a filtered view. Unfortunately, each time the user switched between the “full” view and the “filtered” view they needed to scroll back down to the repeating section that contains the data. I needed a way to automatically set focus on the repeating section after the user switched between views.

I immediately looked for a Focus method on the InfoPath control classes, but no dice. Luckily, the InfoPath Team Blog has a post about programmatic selection in InfoPath. I couldn’t get the SelectText method to work, but the SelectNodes method selected the first row in my repeating section exactly as I wanted.

One little gotcha: The InfoPath SDK documentation says that the varViewContext argument of the SelectText and SelectNodes methods is optional, but the actual interop signature doesn’t include an overload without the argument. I’m using “Type.Missing” as my argument value, and it’s working.