Infopath multiple selection check boxes

I had an issue at work where we had to create a checkbox section with some values in Infopath. Whatever a user checks will get sent to the database separated by a semicolon. We decided to use multiple-selection list boxes because we wanted to allow the user to select more than one checkbox. So we defined some rules and created a sample hidden field which will keep track of all the values selected separated by semicolon. The issue came when we had to uncheck a box. There was no rule we could apply to update our text box after a user unchecked a box. So we found this solution:

If you are using infopath 2010, go to developer tab and click on Content Changed Event. This will open up code view with a function already defined for you. You just have to add this code in between the curly brackets:


//Data Source Navigator
XPathNavigator mainXN = this.MainDataSource.CreateNavigator();
//Namespace Manager
XmlNamespaceManager ns = this.NamespaceManager;
//Node Itterator, so we can loop through the selected items
XPathNodeIterator xi = mainXN.Select("/dfs:myFields/my:BoxMulti", ns); //replace BoxMulti with the name of your multiple selection box
//Reference to the field we want to add the selected items to
XPathNavigator semicolonList = mainXN.SelectSingleNode("/dfs:myFields/my:textfield", ns); //replace textfield with the name of your hidden text field
//Clear the field before we write to it. This will cover unchecking an item, too.
semicolonList.SetValue("");
//Loop through the selected item.
while (xi.MoveNext())
{
//If the item is not blank
if (xi.Current.InnerXml != "")
{
//Concatenate the item to the field, separated by a semicolon
semicolonList.SetValue(semicolonList.Value + xi.Current.InnerXml + "; ");
}
}

Hope this works for you!

If you want to create a simple multiple selection list box without code, you can follow instructions on this site:

http://office.microsoft.com/en-us/infopath-help/insert-a-multiple-selection-list-box-HA010081961.aspx

Advertisement
This entry was posted in C#, infopath and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s