# How to programmatically enable/disable a slider in Manipulate?

in my code below, I have 2 variables a and b, and a option whether it is on or off state. I would like to disable slider for variable b (and therefore it would take default value = 0 in that case) when myOptions = "Off". In other words, I don't want the user to be able to change the value of b when myOptions is set to "Off". And also I would like the slider for variable "b" disappear too. How can I do that ?

Manipulate[
a x + b, {a, -1, 1}, {{b, 0}, -1, 1}, {myOptions, {"On", "Off"}}
]

• – Anjan Kumar Mar 6 '17 at 2:41
• It is generally considered to be bad practice in user interface design circles to make a control disappear when it is disabled. Many users find such behavior upsetting. Some even think they have done something that has broken the application or even the computer itself. Standard practice is to gray-out a disabled control. – m_goldberg Mar 6 '17 at 7:24
• Thank u a lot for the tip @m_goldberg – N.T.C Mar 6 '17 at 23:25

Enabled can be useful here; the Slider will not disappear but it will be "grayed out."

Manipulate[If[myOptions == "Off", b = 0];
a x + b, {a, -1, 1}, {{b, 0}, -1, 1,
Enabled -> (myOptions === "On")}, {myOptions, {"On", "Off"}}]


By using Dr. belisarius's method and Column (to make the alignment consistent), we get,

Manipulate[a x + b, Column@{Control@{a, -1, 1}},
Dynamic@If[myOptions == "On", Column@{Control@{{b, 0}, -1, 1}},
b = 0; Row@{}], {myOptions, {"On", "Off"}}]


You can just use an If to lock the b variable to zero:

Manipulate[If[myOptions == "Off", b = 0];
a x + b, {a, -1, 1}, {{b, 0}, -1, 1}, {myOptions, {"On", "Off"}}]

• But the slider for variable b is still there, I want to make it disappear from the UI so that it's less confusing. – N.T.C Mar 6 '17 at 1:33
• That's not what you asked in your question -- you asked for the user not to be able to change b. – bill s Mar 6 '17 at 1:40
• Sorry, I've edited my question to reflect that what I wanted. – N.T.C Mar 6 '17 at 2:04