How to programmatically retrieve selected items in a Multiple-Selection list box in InfoPath

Learn how you can write C# code to get the values or display names of the items that were selected in a Multiple-Selection List Box on an InfoPath form.

Suppose you have a Multiple-Selection List Box named myListBox on an InfoPath form and you would like to click a Button and fill a Text Box named myTextBox with the values or display names of the items that were selected in the Multiple-Selection List Box.

Retrieving the values of selected items

You could use the following C# code to retrieve the values that were selected:

XPathNavigator root = MainDataSource.CreateNavigator();
XPathNodeIterator iter = root.Select("//my:myListBox",
  NamespaceManager);

System.Text.StringBuilder sb = new System.Text.StringBuilder();

while (iter.MoveNext())
{
  string value = iter.Current.Value;
  sb.Append(value);
  sb.Append(";");
}

root.SelectSingleNode("//my:myTextBox",
  NamespaceManager).SetValue(sb.ToString());

This code would give you the Values of the items that were selected and not the Display Names of the items unless you set the Display Names to be the same as the Values of items.

Retrieving the display names of selected items

If you want to retrieve the Display Names of the items that were selected and you’ve bound the Multiple-Selection List Box to a repeating node either in the Main data source or a secondary data source, you’ll have to do a programmatic lookup to retrieve the Display Names.

You could use the following C# code to retrieve the display names of the items that were selected:

XPathNavigator root = MainDataSource.CreateNavigator();
XPathNodeIterator iter = root.Select("//my:myListBox",
  NamespaceManager);

System.Text.StringBuilder sb = new System.Text.StringBuilder();

while (iter.MoveNext())
{
  string value = iter.Current.Value;

  XPathNavigator secDS = DataSources["Fruits"].CreateNavigator();
  string displayName = secDS.SelectSingleNode(
    "/dfs:myFields/dfs:dataFields/d:Fruits[@ID = '"
    + value + "']/@Name", NamespaceManager).Value;

  sb.Append(displayName);
  sb.Append(";");
}

root.SelectSingleNode("//my:myTextBox",
 NamespaceManager).SetValue(sb.ToString());

where Fruits is a secondary data source, @ID the value of an item, and @Name the display name of an item.

Note: You cannot do a lookup for items if you’ve manually entered them into the list box.

Other useful articles about list boxes

InfoPath Basics: List control basics in InfoPath
This article explains how to use list controls such as drop-down list boxes, combo boxes, list boxes, and multi-select list boxes in InfoPath.

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.

Programmatically move items between two lists in InfoPath using C# code
Learn how to copy items from a secondary data source to a list in the Main data source of a form and then move items between two lists which are bound to two repeating groups in the Main data source of the form template.

Move items between two InfoPath list boxes (no code)
Watch this InfoPath 2013 demo video of how you can use rules to move items between two list box controls on an InfoPath form without having to write code.

 
 Subscribe via RSS or Email

Related Posts

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.

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