4
$\begingroup$

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"}}
]
$\endgroup$
3
  • $\begingroup$ Check mathematica.stackexchange.com/questions/40037/… $\endgroup$ Commented Mar 6, 2017 at 2:41
  • $\begingroup$ 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. $\endgroup$
    – m_goldberg
    Commented Mar 6, 2017 at 7:24
  • $\begingroup$ Thank u a lot for the tip @m_goldberg $\endgroup$
    – N.T.C
    Commented Mar 6, 2017 at 23:25

3 Answers 3

10
$\begingroup$

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"}}]
$\endgroup$
6
$\begingroup$

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"}}]

enter image description here enter image description here

$\endgroup$
3
$\begingroup$

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"}}]
$\endgroup$
3
  • $\begingroup$ But the slider for variable b is still there, I want to make it disappear from the UI so that it's less confusing. $\endgroup$
    – N.T.C
    Commented Mar 6, 2017 at 1:33
  • 1
    $\begingroup$ That's not what you asked in your question -- you asked for the user not to be able to change b. $\endgroup$
    – bill s
    Commented Mar 6, 2017 at 1:40
  • $\begingroup$ Sorry, I've edited my question to reflect that what I wanted. $\endgroup$
    – N.T.C
    Commented Mar 6, 2017 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.