Conditional formatting: An overview

Applies to
Microsoft Office InfoPath® 2003 Service Pack 1 or later

You can use conditional formatting to change the appearance of a control based on values that users enter into your InfoPath form. This article provides an overview of how to use conditional formatting to add dynamic elements to your form and offers examples of conditional formatting:

What is conditional formatting?

Use color to indicate status in a repeating section

Highlight data in a repeating table

Create a dynamic Help window

Filter rows in a repeating table

Set pattern-matching conditions for data

Disable or set a control as read-only

What is conditional formatting?

In InfoPath, conditional formatting allows you to specify different formatting options for a control based on logical conditions that occur on the form. If the specified conditions are present when a user fills out the form, the conditional formatting is displayed. You can use conditional formatting to apply bold, underline, italic, strikethrough, font color, and background color to controls. You can also use conditional visibility, which is a type of conditional formatting that allows you to specify when a control is visible on the form. Both conditional formatting and conditional visibility can be applied to the following controls:

The following examples demonstrate how you can use conditional formatting in InfoPath forms.

Top of Page Top of Page

Use color to indicate status in a repeating section

You can use color in a repeating section to indicate status. For example, you can set a different background color for each item in a repeating section. The color that is displayed depends on the data that a user enters into the control within that section. In the example, the color of each row in a repeating section is based on the completion status that the user chooses for that row.

Stop Light coloring example

If you want to use color on your form to indicate the status in a repeating section, you must first insert a repeating section that contains a text box and a drop-down list box:

  1. Create a new, blank form.

ShowHow?

  1. In design mode, click Design a Form on the File menu.
  2. In the Design a Form task pane, click New Blank Form.
  1. Insert a repeating section on the form.

ShowHow?

  1. Place the insertion point where you want to insert the repeating section.
  2. If the Controls task pane is not visible, click More Controls on the Insert menu.
  3. In the Controls task pane, select the Automatically create data source check box.
  4. Under Insert controls, click Repeating Section.
  1. Insert a text box and a drop-down list box inside the repeating section.

ShowHow?

  1. Place the insertion point in the repeating section.
  2. Under Insert controls in the Controls task pane, click Text Box.
  3. Press ENTER, and then under Insert controls, click Drop-Down List Box.
  1. Type Work Item: as the label for the text box.
  2. Type Status: as the label for the drop-down list box.
  3. Double-click the drop-down list box on the form, and then under List box entries in the Drop-Down List Box Properties dialog box, click Add.
  4. In the Value box in the Add Choice dialog box, type Active.
  5. Repeat the preceding step to add Resolved and Closed as choices in the drop-down list box.

After you insert the text box and drop-down list box into your form, you can specify the conditions that you want to use for the conditional formatting:

  1. Select the repeating section, and then on the Format menu, click Conditional Formatting.
  2. To add a condition, click Add in the Conditional Formatting dialog box.
  3. Under If this condition is true in the first drop-down list box, select the field that corresponds to the drop-down list box that you just inserted.
  4. In the second drop-down list box, leave the value as is equal to.
  5. In the third drop-down list box, select Type text, and then type Active.

 Note   The expressions are case sensitive.

  1. Under Then apply this formatting in the Shading list, select red, and then click OK.
  2. Repeat these steps to add conditional formatting for the two remaining list entries. Select yellow as the background color for Resolved and green as the background color for Closed.

Finally, preview and test the conditional formatting:

  1. On the Standard toolbar, click Preview Form.
  2. In the preview window, select the different options in the drop-down list. Notice that as you select the options, the background color in the Work Item box changes accordingly.
  3. On the Insert menu, point to Section, and then click the section name to add an additional section. Notice that the conditional formatting is applied independently to both sections.

Top of Page Top of Page

Highlight data in a repeating table

You can also use conditional formatting to highlight data in a repeating table. Repeating tables function like repeating sections, but when a condition is set for a repeating table, each row of the table has the condition applied independently. For example, if you have a repeating table that contains a list of employees and the number of years that each has been with the company, you can use conditional formatting to apply different colors to each row, depending on the length of the person's employment. If an employee has been with the company for more than 10 years, the row will be orange, and if the employee has been with the company for more than 15 years, the row will be red.

To create this type of conditional formatting, you must first insert a repeating table:

  1. Create a new, blank form.

ShowHow?

  1. In design mode, click Design a Form on the File menu.
  2. In the Design a Form task pane, click New Blank Form.
  1. Insert a repeating table on the form.

ShowHow?

  1. Place the insertion point where you want to insert the repeating table.
  2. If the Controls task pane is not visible, click More Controls on the Insert menu.
  3. Under Insert controls in the Controls task pane, click Repeating Table.
  1. When you insert a repeating table, you will be prompted for the number of columns. Leave the default value at 3, and then click OK.
  2. Add column labels in the first row of the table, so that the first column is labeled Last Name, the second is labeled First Name, and the third is labeled Total Years.

After you insert the repeating table into your form, you can specify the conditions for the conditional formatting:

  1. Select the repeating table, and then on the Format menu, click Conditional Formatting.
  2. To add a condition, click Add in the Conditional Formatting dialog box.
  3. Under If this condition is true in the Conditional Format dialog box, in the first drop-down list box, select the field that corresponds to the text box in the third column of the repeating table.
  4. In the second drop-down list box, select is greater than or equal to.
  5. In the third drop-down list box, select Type a number, and then type 10.
  6. Under Then apply this formatting in the Shading list, select orange, and then click OK to save the first condition.
  7. Repeat these steps to add the second condition. This time, type 15 instead of 10, and then select red as the background shading color.
  8. In the Conditional Formatting dialog box, arrange the conditions by using the Move Up and Move Down buttons, so that the greater than 15 condition is at the top of the list of conditions. Conditions are applied in order from top to bottom. Therefore, if the greater than 10 condition is on top, the greater than 15 condition will never be applied, because anything that is greater than 15 is also greater than 10.

Finally, preview the form and test the conditional formatting:

  1. On the Standard toolbar, click Preview Form.
  2. In the preview window, add several rows to the table, and then type numbers in the third column to ensure that the conditions are being applied correctly.

Top of Page Top of Page

Create a dynamic Help window

Conditional visibility is a type of conditional formatting that allows you to hide a control on a form. It is a powerful tool for building dynamic forms. In the following example, you will learn how to build a form that, when first viewed, shows a window that contains instructions for filling out the form. When a user selects the check box in the window, the window closes and the actual form appears.

 Note   It is important to remember that instructions in the window are hidden when the user selects the check box and are not actually removed from the form. To add controls to a form that a user can actively add and remove, use optional sections instead.

To apply this type of conditional formatting, you must first insert two sections and a check box:

  1. Create a new, blank form.

ShowHow?

  1. In design mode, click Design a Form on the File menu.
  2. In the Design a Form task pane, click New Blank Form.
  1. Insert a check box and two sections on the form.

ShowHow?

  1. Place the insertion point where you want to insert the check box.
  2. If the Controls task pane is not visible, click More Controls on the Insert menu.
  3. Under Insert controls in the Controls task pane, click Check Box.
  4. Press ENTER, and then under Insert controls, click Section twice.
  1. Select the field name that is automatically inserted next to the check box, and then type Fill out the form?.
  2. In the first section that you inserted, type Here is how to fill out the form:.

 Note   This section will contain the instructions.

  1. Insert a text box in the second section that you inserted, and then add a label in front of the text box that reads Name:.

 Note   This section will contain the actual form that the user will fill out.

After you insert the check box and the two sections into your form, you can specify the conditions for the conditional visibility formatting:

  1. Select the second section, and then on the Format menu, click Conditional Formatting.
  2. To add a condition, click Add in the Conditional Formatting dialog box.
  3. Under If this condition is true in the first drop-down list box, select Select a field or group.
  4. In the Select a Field or Group dialog box, click the field that corresponds to the check box in the first section, and then click OK.
  5. In the third drop-down list box in the Conditional Formatting dialog box, select FALSE, and then under Then apply this formatting, select the Hide this control check box.

 Note   Hiding a section is different from removing it. If you save the form while a section is hidden, the contents of that hidden section are saved as part of the form data.

  1. To save the condition, click OK twice.
  2. Select the first section on the form, and then repeat the preceding steps, but this time, under If this condition is true in the third drop-down list, select True.

Finally, preview the form and test the conditional formatting:

  1. On the Standard toolbar, click Preview Form.

 Note   By default, only the Help window and the check box are visible.

  1. Select and clear the check box, and notice how the sections change visibility.
  2. With the check box selected, type some text in the Name text box.
  3. Clear and then select the check box, and notice that the Name text box still contains the text that you typed in step 3.

Top of Page Top of Page

Filter rows in a repeating table

You can use conditional visibility to filter items in a repeating table or repeating section. For example, if a form has a repeating table that contains rows of products that fall into one of three classifications (A, B, or C), you can filter the rows based on the classification of the products.

In order to create this type of filter, you must use a list box with conditional visibility to display only the rows that match the type that is selected in the list box.

To create this type of filter, you must first insert a list box and a repeating table:

  1. Create a new, blank form.

ShowHow?

  1. In design mode, click Design a Form on the File menu.
  2. In the Design a Form task pane, click New Blank Form.
  1. Insert a list box and a repeating table on the form.

ShowHow?

  1. Place the insertion point where you want to insert the list box.
  2. If the Controls task pane is not visible, click More Controls on the Insert menu.
  3. Under Insert controls in the Controls task pane, click List Box.
  4. Press ENTER, and then under Insert controls, click Repeating Table.
  1. When you insert a repeating table, you will be prompted for the number of columns. Leave the default value at 3, and then click OK.
  2. Above the list box that you inserted, type a label that reads Filter by Type:.
  3. Type column labels in the first row of the repeating table, so that the first column reads Product, the second column reads Type, and the third column reads Price.
  4. Double-click the list box, and then under List box entries in the List Box Properties dialog box, type A, B, C, and D as the list entries.

ShowHow?

  1. Under List box entries, click Add.
  2. In the Add Choice dialog box, type a value, and then click OK.
  1. In the list of values, select D, and then click Set Default.

After you insert the list box and the repeating table into your form, you can specify the conditions for the conditional visibility formatting:

  1. Select the repeating table, and then on the Format menu, click Conditional Formatting.
  2. To add a condition, click Add in the Conditional Formatting dialog box.
  3. Under If this condition is true in the first drop-down list box, select the field that corresponds to the second column of the repeating table.
  4. In the second drop-down list box, select is not equal to.
  5. In the third drop-down list box, select Select a field or group.
  6. In the Select a Field or Group dialog box, select the field that corresponds to the list box that you inserted on the form, and then click OK.

 Note   The condition will dynamically compare the two values, which is different from the static comparisons in the previous examples.

  1. Under Then apply this formatting in the Conditional Format dialog box, select the Hide this control check box.

 Note   This condition now states that if the content of the second column of a repeating table row is different from what is selected in the list box, the corresponding rows should be hidden.

  1. Double-click the field in the second column of the repeating table, and then in the Text Box Properties dialog box, under Default Value, type D in the Value box.

Finally, preview the form and test the conditional formatting:

  1. On the Standard toolbar, click Preview Form.

 Note   By default, the selection in the list box is D. You might need to scroll up in the text box to see the other values.

  1. Under the repeating table, click Insert item several times to add a few rows to the repeating table.
  2. For each row in the second column of the table, replace the default value of D with A, B, and C.

 Note   The expressions are case sensitive.

  1. Change the selection in the list box. The rows in the repeating table will appear and disappear as you select different options.

Top of Page Top of Page

Set pattern-matching conditions for data

Conditional formatting can be used to help ensure that the data that a user enters into a specific control matches a predetermined pattern. For example, you can ensure that users always type a prefix when they type a part number.

To use this type of conditional formatting, insert a text box:

  1. Create a new, blank form.

ShowHow?

  1. In design mode, click Design a Form on the File menu.
  2. In the Design a Form task pane, click New Blank Form.
  1. Insert a text box on the form.

ShowHow?

  1. Place the insertion point on the form where you want to insert the text box.
  2. If the Controls task pane is not visible, click More Controls on the Insert menu.
  3. Under Insert controls in the Controls task pane, click Text Box.

After you insert the text box into your form, you can specify the conditions for the conditional visibility formatting:

  1. Select the text box that you just inserted, and then on the Format menu, click Conditional Formatting.
  2. To add a condition, click Add in the Conditional Formatting dialog box.
  3. Under If this condition is true in the first drop-down list box, select Select a field or group.
  4. In the Select a Field or Group dialog box, click the field that corresponds to the text box, and then click OK.
  5. In the second drop-down list box in the Conditional Format dialog box, select does not match pattern.
  6. In the third drop-down list box, select Select a pattern.
  7. Under Standard patterns in the Data Entry Pattern dialog box, click Custom pattern.
  8. In the Custom pattern box, type \p{L}\p{L}-\d\d\d\d\d and then click OK.

 Note   In the pattern, \p{L} represents any letter, and \d represents any digit.

  1. Under Then apply this formatting in the Shading list, select red, and then click OK twice to save the condition and return to the form.

This condition now states that, if the content of the text box does not match the predefined pattern (two letters, followed by a hyphen, followed by five numbers), the background for the section will be colored red.

Finally, preview the form and test the conditional formatting:

  1. On the Standard toolbar, click Preview Form.

 Note   By default, the text box does not contain a number that matches the pattern that you set. Therefore, the background color of the section is red.

  1. In the text box in the preview window, type AB-12345.
  2. Click outside the text box. The background will change from red to the default background color of the form.

Top of Page Top of Page

Disable or set a control as read-only

Conditional formatting can also be used to help ensure that a user's ability to use a specific control is based on the state of another control. For example, you can use conditional formatting to set a text box as read-only unless the user makes a selection from a drop-down list box. You might use this approach to ensure that users cannot type their name until after they specify a title such as Ms. or Mr. Similarly, you can use conditional formatting to help ensure that a user is unable to select a specific control based on the state of another control. For example, you can set a button control as disabled if a check box control is not selected. You might use this type of conditional formatting to ensure that users cannot click a button unless they signify their acceptance of a user agreement.

In the following example, you'll use conditional formatting to disable or enable a file attachment control, depending on whether a check box is selected.

First, insert a check box and a file attachment control:

  1. Create a new, blank form.

ShowHow?

  1. In design mode, click Design a Form on the File menu.
  2. In the Design a Form task pane, click New Blank Form.
  1. Insert a check box and a file attachment control on the form.

ShowHow?

  1. Place the insertion point on the form where you want to insert the check box.
  2. If the Controls task pane is not visible, click More Controls on the Insert menu.
  3. Under Insert controls in the Controls task pane, click Check Box.
  4. Press ENTER, and then under File and Picture, click File Attachment.

After you insert the check box and file attachment control into your form, you can specify the conditions for the conditional formatting:

  1. Select the file attachment control, and then on the Format menu, click Conditional Formatting.
  2. To add a condition, click Add in the Conditional Formatting dialog box.
  3. Under If this condition is true in the first drop-down list box, select Select a field or group.
  4. In the Select a Field or Group dialog box, click the field that corresponds to the check box, and then click OK.
  5. In the second drop-down list box in the Conditional Format dialog box, select Is equal to.
  6. In the third drop-down list box, select TRUE.
  7. Under Then apply this formatting, click Disable this control.
  8. To save the condition and return to the form, click OK twice.

Finally, preview the form and test the conditional formatting:

  1. On the Standard toolbar, click Preview Form.
  2. In the preview window, select the check box on the form.

The file attachment control will become disabled.

  1. To attach a file, clear the check box.

Conditional formatting is also a useful tool when you want to help ensure that a user is unable to type in a specific control based on the state of another control. In the following example, you will use this type of conditional formatting to make a text box read-only when a user selects a specific check box on the form:

  1. Create a new, blank form.

ShowHow?

  1. In design mode, click Design a Form on the File menu.
  2. In the Design a Form task pane, click New Blank Form.
  1. Insert a check box and text box on the form.

ShowHow?

  1. Place the insertion point on the form where you want to insert the check box.
  2. If the Controls task pane is not visible, click More Controls on the Insert menu.
  3. Under Insert controls in the Controls task pane, click Check Box.
  4. Press ENTER, and then under Insert controls in the Controls task pane, click Text Box.

After you insert the check box and text box into your form, you can specify the conditions for the conditional formatting:

  1. Select the text box, and then on the Format menu, click Conditional Formatting.
  2. To add a condition, click Add in the Conditional Formatting dialog box.
  3. Under If this condition is true in the first drop-down list box, select Select a field or group.
  4. In the Select Field or Group dialog box, click the field that corresponds to the check box, and then click OK.
  5. In the second drop-down list box in the Conditional Format dialog box, select Is equal to.
  6. In the third drop-down list box, select TRUE.
  7. Under Then apply this formatting, click Read-only.

 Note   If you want to hide the text box when the check box is selected, select Hide this control.

  1. To save the condition and return to the form, click OK twice.

Finally, preview the form and test the conditional formatting:

  1. On the Standard toolbar, click Preview Form.
  2. In the preview window, select the check box on the form.

The text box will change to read-only.

  1. Try to type in the text box. You should not be able to type in the text box.
  2. To type in the text box, clear the check box.

Top of Page Top of Page

 
 
Applies to:
InfoPath 2003