How to programmatically disable/enable a control on an InfoPath form

Use code to programmatically set the value of a control that will trigger Conditional Formatting on the control that you want to disable/enable.

Problem

You have an InfoPath form template with a control, such as a drop-down list box, which you would like to programmatically disable and enable.

Solution

Use code to programmatically set the value of another control that will trigger Conditional Formatting on the control that you want to disable/enable.

Discussion

You can accomplish this functionality as follows:

  1. Add a Drop-Down List Box control, a Check Box control, and two Button controls to an InfoPath form template.
  2. Double-click the Check Box control to open its Properties dialog box.
  3. On the Data tab, in the Field name field type isControlEnabled, select 0 for Value when cleared and 1 for Value when checked, and click OK.
  4. Double-click the Drop-Down List Box control to open its Properties dialog box.
  5. On the Display tab, click Conditional Formatting.
  6. On the Conditional Formatting dialog box, click Add.
  7. On the Conditional Format dialog box, select isControlEnabled from the first drop-down list box, leave is equal to in the second drop-down list box, select "0" from the third drop-down list box, tick Disable this control, and click OK.
  8. Click OK on all the dialog boxes that are currently open.
  9. Double-click the first Button control to open its Properties dialog box.
  10. On the General tab, in the Label field type Disable, and then click Edit Form Code to add a Clicked event handler for the button.
  11. Add the following code to the Clicked event handler for the Disable Button control: MainDataSource.CreateNavigator().SelectSingleNode("//my:isControlEnabled", NamespaceManager).SetValue("0");
  12. Double-click the second Button control to open its Properties dialog box.
  13. On the General tab, in the Label field type Enable, and then click Edit Form Code to add a Clicked event handler for the button.
  14. Add the following code to the Clicked event handler for the Enable Button control: MainDataSource.CreateNavigator().SelectSingleNode("//my:isControlEnabled", NamespaceManager).SetValue("1");
  15. Save and build your project.
  16. Publish the form template to a SharePoint Server running Forms Services. Since the form template contains managed code, you will have to perform an administrator-approved InfoPath form template deployment.

You should now be able disable or enable the drop-down list box control when you click either the Disable or Enable button.

Tip:

You can also toggle between the two states of a control (disabled or enabled) by using the following code in the event handler of one of the Button controls: XPathNavigator domNav = MainDataSource.CreateNavigator();
if (domNav.SelectSingleNode("//my:isControlEnabled", NamespaceManager).Value == "1")
domNav.SelectSingleNode("//my:isControlEnabled", NamespaceManager).SetValue("0");
else
domNav.SelectSingleNode("//my:isControlEnabled", NamespaceManager).SetValue("1");

 
 Subscribe for updates via RSS or Email

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