Programmatically select all items in a multiple-selection (multi-select) list box
Use C# or Visual Basic code to loop through all of the items in the secondary data source a multi-select list box is bound to, and add each item to the DOM node that represents the multi-select list box.
You have a multi-select list box that is bound to an XML document. Since all items are displayed without a checkmark, you would like to add a checkmark to all items when you click a button.
Retrieve the contents of the XML document (=secondary data source) the multi-select list box is bound to, loop through all of its items, and add an item to the DOM node that represents the multi-select list box for each item in the XML document.
Warning: Each item added to the multi-select list box must contain the Value that was defined in the XML document for that item. If you use the Display name instead of the Value when adding items to the multi-select list box, duplicate items may appear in the multi-select list box.
You can accomplish this functionality as follows:
- Open Microsoft Office InfoPath 2007, create a new blank form, and add a Multiple-Selection List Box
and a Button control to the form. Your form could resemble the following figure:
Figure 1. The the form template in Design mode.
- Create a Players XML document, add a Receive data connection for this XML document
to the InfoPath form template, and bind
the Multiple-Selection List Box to the data connection you created. In this article,
players is the name of the secondary data source, number is the number of
a player and the node used as the Value of the multi-select list box, and name
is the name of a player and the node used as the Display name of the multi-select list box.
Figure 2. Binding the multiple-selection list box to a secondary data source.
- Add the following code to the Clicked event handler of the Select All button:
XPathNodeIterator players = DataSources["players"].CreateNavigator().Select(
foreach (XPathNavigator player in players)
string number = player.SelectSingleNode("number", NamespaceManager).Value;
Dim players As XPathNodeIterator = DataSources("players").CreateNavigator().Select( _
Dim player As XPathNavigator
For Each player In players
Dim number As String = player.SelectSingleNode("number", NamespaceManager).Value
- Add the following code for the AddItem method, which is used to add an item to the multi-select
list box, to the form's code:
private void AddItem(string itemId)
XPathNavigator DOM = MainDataSource.CreateNavigator();
XPathNavigator group1 = DOM.SelectSingleNode("//my:group1", NamespaceManager);
XPathNavigator field1 = DOM.SelectSingleNode("//my:group1/my:field1", NamespaceManager);
XPathNavigator newNode = field1.Clone();
Public Sub AddItem(ByVal itemId As String)
Dim DOM As XPathNavigator = MainDataSource.CreateNavigator()
Dim group1 As XPathNavigator = DOM.SelectSingleNode("//my:group1", NamespaceManager)
Dim field1 As XPathNavigator = DOM.SelectSingleNode("//my:group1/my:field1", NamespaceManager)
Dim newNode As XPathNavigator = field1.Clone()
- Save your work, compile the code, and test the form.
You should now have a fully functional form so that when you click on the Select All button, all of the items in the multiple-selection list box display a checkmark.
Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.
Related InfoPath Articles:
- InfoPath Basics: List control basics in InfoPath
- Pre-select items in a Multiple-Selection List Box in InfoPath 2010
- Multi-select list box to show/hide columns in horizontal repeating table
- Understanding the Multiple-Selection List Box in InfoPath
- Cascading multi-select list boxes with validation in InfoPath 2013