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.
Problem
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.
Solution
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.
Discussion
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 (also see: Insert a multiple-selection list box). Your form could resemble the following figure:
Figure 1. The the form template in Design mode. - Create an 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
(also see: Working with the data source and data connections). 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
(also see: How to: Add an Event Handler):
C#XPathNodeIterator players = DataSources["players"].CreateNavigator().Select(
"//player", NamespaceManager);
foreach (XPathNavigator player in players)
{
string number = player.SelectSingleNode("number", NamespaceManager).Value;
AddItem(number);
}
Visual BasicDim players As XPathNodeIterator = DataSources("players").CreateNavigator().Select( _
"//player", NamespaceManager)
Dim player As XPathNavigator
For Each player In players
Dim number As String = player.SelectSingleNode("number", NamespaceManager).Value
AddItem(number)
Next - 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:
C#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();
newNode.SetValue(itemId);
group1.AppendChild(newNode);
}
Visual BasicPublic 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()
newNode.SetValue(itemId)
group1.AppendChild(newNode)
End Sub - 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.
Related InfoPath Articles:
