BPC-Components

Home > Products > BPC PowerForms > Knowledge Base > How To Articles > Enable or Disable a control based on form conditions

Enable or Disable controls based on form conditions

One of the most common scenarios when designing a custom form, is the requirement to enable/disable a specific control (or set of controls) based on a) the values of other controls, b) the user using the form.

For our example, we have a list with the following columns :

Customer (lookup column referencing the Customers list)
Priority (Choice column with values : Normal, High, Low)

The scenario requires that if a specific customer is selected, the Priority ComboBox should be set to Priority=High and should be disabled for the user.

When first opening the form, it should show something like the following :

 

You can enable or disable controls using 2 different approaches :

Enabled Formula

Open the designer and select the Priority ComboBox properties.

In the Enabled formula add the following expression :

Code

form.GetControl("c_Customer").GetValue_Name() != "BPC"

NOTE : In the expression above, we only check for the display member of the combobox value. If we wanted to check for the value member (ID) we would have to write something like :

Code

form.GetControl("c_Customer").GetValue_Value() != "1"

// or

form.GetControl("c_Customer").GetValue() != "1;#BPC"

When the user selects a different customer, the Priority combo is enabled :

But if we select the customer appearing in the formula, the control is automatically disabled :

Now to completely meet the requirements, we have to set the value to "High".

This can be accomplished using a simple script inside the Value-Change event of the Customer ComboBox.

The script should be something like the following :

Code

if (form.Loaded) {

  value = form.GetControl("c_Customer").GetValue_Name();

  if (value == "BPC") form.GetControl("c_Priority").SetValue("High");

}

 

The form.Loaded condition is used because we want this script to be executed only after the user changes the value and not during form initialization (the value-change events are raised during form loading).

Using script

The same thing can be accomplished without using the Enabled formula but by script alone.

Here is the changed script that we should use to have the same effect.

The script should handle the value-change event of the Customer ComboBox.

Code

value = form.GetControl("c_Customer").GetValue_Name();

if (value == "BPC") {

form.GetControl("c_Priority").SetEnable(false);

if (form.Loaded) form.GetControl("c_Priority").SetValue("High");

}

else {

  form.GetControl("c_Priority").SetEnable(true);

}

 

Notes :

In the above script, we check the customer value and we set the Enabled property of the Priority control.

We set the default value only when the event was raised from a user action and not due to form initialization.