Create text boxes for items selected in a multiple-selection list box in InfoPath 2010
Learn how you can use conditional formatting to hide and show rows of a repeating table that are based on the items selected in a multiple-selection list box on an InfoPath 2010 form.
You cannot create text boxes on an InfoPath form at runtime simply because you cannot dynamically create fields that are not already present in the Main data source of an InfoPath form and that do not adhere to the schema that was defined during design time of the InfoPath form template.
An exception to the above is if you write code to add rows to a repeating table which is already part of the schema of the InfoPath form, or write code to add items to a drop-down list box which is bound to a repeating group that is already part of the Main or a secondary data source.
However, you can pre-add text boxes, hide them, and then use conditional formatting to show them when you require them. So let’s look at how you can go about doing this.
Suppose you have a multiple-selection list box on an InfoPath form that contains 5 items: 1=Last Name, 2=First Name, 3=Street, 4=City, 5=Country.
Figure 1. Multiple-selection list box with 5 static items in InfoPath 2010.
Suppose now that you want to have a text box appear corresponding to any of these items whenever an item is selected from the multiple-selection list box.
You can use a repeating table that is prefilled with 5 rows, with each row containing a label and a text box corresponding to one of the selected items. And you can use a hidden column in the repeating table to keep track of which row pertains to which item in the multi-select list box.
And did you know that you can format a repeating table to be completely invisible on an InfoPath form? Here is how you do it:
- Add a Repeating Table control with 3 columns to the InfoPath form template.
- Right-click the first text box and select Change Control > Calculated Value.
- Click the repeating table to select it, and then click Data > Form Data > Default Values.
- On the Edit Default Values dialog box, expand group2, add 4 group3 nodes, and fill field2 and field4 of each group3 with the corresponding display name and value of an item in the multiple-selection list box, and click OK.
- Right-click the repeating table and select Repeating Table Properties.
- On the Repeating Table Properties dialog box, deselect the Allow users to insert and delete rows check box, and click OK.
- Right-click the third column of the repeating table (containing field4), and select Delete > Columns. This effectively makes this column a hidden column in the repeating table.
- Right-click the repeating table and select Borders and Shading.
- On the Borders and Shading dialog box on the Border tab, select None under Presets, select white (or any other color you are using as the background color for the view) from the Color picker, click the Shading tab, select No color, and click OK. This makes the repeating table borderless and colorless.
Figure 2. Borderless repeating table in InfoPath 2010.
- Now you must use conditional formatting to make the repeating table invisible. Click the repeating table to select it, and then click Home > Rules > Manage Rules.
- On the Rules task pane, click New > Formatting.
- On the Rules task pane under Condition, click None.
- On the Condition dialog box, select Select a field or group from the first drop-down list box.
- On the Select a Field or Group dialog box, expand group1, select field1 (representing the multiple-selection list box), and click OK.
- On the Condition dialog box, leave is equal to selected in the second drop-down list box, and select Select a field or group from the third drop-down list box.
- On the Select a Field or Group dialog box, expand group2, expand repeating group3, select field4 (contains a value equal to that of an item in the multiple-selection list box), and click OK.
- On the Condition dialog box, select The expression from the first drop-down list box, and then type not( and ) around the expression in the text box. This condition is a negation for checking the value of selected items in the multiple-selection list box. The final expression should resemble the following:
not(../../my:group1/my:field1 = my:field4)
- On the Rules task pane under Formatting, select the Hide this control check box. So the final rule hides a row in the repeating table if none of the values of the selected items in the multiple-selection list box are equal to the value of field4 in that row of the repeating table.
- Preview the form.
When the form opens, the repeating table should be invisible, and when you select any item from the multiple-selection list box, a text box with a label should appear. Note that you could also pre-select items in the multiple-selection list box to have specific rows automatically added to the repeating table when the form opens.
The order of the text boxes remains the same as the order in which the rows where defined when you added default rows to the repeating table, and does not depend on the order in which you select items in the multi-select list box.
Figure 3. Text boxes for selected items in multi-select list box in InfoPath 2010.
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.