Programmatically select all items in a multiple-selection (multi-select) list box

Applies to: InfoPath 2007

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.

ADVERTISEMENTS

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:

  1. 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.
  2. 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.
  3. Add the following code to the Clicked event handler of the Select All button:

    C#
    XPathNodeIterator players = DataSources["players"].CreateNavigator().Select(
    "//player", NamespaceManager);

    foreach (XPathNavigator player in players)
    {
    string number = player.SelectSingleNode("number", NamespaceManager).Value;
    AddItem(number);
    }


    Visual Basic
    Dim 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
  4. 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 Basic
    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()
    newNode.SetValue(itemId)
    group1.AppendChild(newNode)
    End Sub
  5. 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:

 

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. 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.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services