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

by S.Y.M. Wong-A-Ton | Filed under: InfoPath 2007

Learn how InfoPath coding differs from normal .NET coding when it comes to enabling and disabling controls on an InfoPath form.

If you are a long-time programmer who has been building ASP.NET or WinForms applications for a while, programming will not seem as intuitive when you switch to working with InfoPath.

Many programmers fall in the trap of wanting to programmatically set properties on controls, because they have come to expect this from other development environments. Well… InfoPath is not a development environment; it is a Microsoft Office desktop application. Visual Studio and VSTA just enable you to write code for InfoPath form templates. So while InfoPath controls do have properties, they cannot be set programmatically. Things work a little bit different in InfoPath. :)

If you want to programmatically disable a control called txtMyTextBox in .NET, it would be just a matter of using the following code:

txtMyTextBox.Enabled = false;

In InfoPath, you have to use Conditional Formatting to be able to disable a control. And since you cannot set conditional formatting directly from code, you will have to use a helper control to set its value from code and then base the disabling/enabling of the control that you want to disable on the value of the helper control.

Clear as mud? I’ll walk you through the steps in How to programmatically disable/enable a control on an InfoPath form.

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. Usage of 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 2010 Cookbook

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010