# How to dynamically gray out a slider?

Here's a very simple MWE to show my problem :

Manipulate[
Plot[a t^2 - b t, {t, -1, 1}, Frame -> True, PlotRange -> {{-1, 1}, {-1, 1}}],
{{a, 1, Style["Parameter 1", 10]}, -10, 10, 0.01},
{{b, 1, Style["Parameter 2", 10]}, -10, 10, 0.01}
]


This code produces a simple graphics with two sliders : parameters $a$ and $b$.

I would like to make the second slider to become all grayed out (i.e. unavailable, with $b$ set to 0) if the user moves the first slider to $a \le 0$.

How to achieve this ? This is certainly very basic (?) but I'm yet unable to see how.

• To grey out the slider, add Enabled -> Dynamic[a > 0] as an option to the b variable specification. Dec 20 '16 at 17:05
• @lowriniak, wow ! It works perfectly. However, the $b$ parameter should turn to 0. How to add this constraint ?
– Cham
Dec 20 '16 at 17:14
• Aah ! This actually solves totally my problem ! I suggest that you write a small answer with the code, so I could mark it (and you get more reputation points) !
– Cham
Dec 20 '16 at 17:19

So to expand a little on my comment, to make a slider be greyed out you can use Enabled:

Manipulate[
Plot[a t^2 - b t, {t, -1, 1}, Frame -> True, PlotRange -> {{-1, 1}, {-1, 1}}],
{{a, 1, Style["Parameter 1", 10]}, -10, 10, 0.01},
{{b, 1, Style["Parameter 2", 10]}, -10, 10, 0.01, Enabled -> False}
] From the tutorial AdvancedManipulateFunctionality, see the section Interdependent Controls - this shows some examples of controls depending on each other. This means we can use a in the definition of b:

Note: in my comment I added a Dynamic which I don't think was strictly necessary

Manipulate[
Plot[a t^2 - b t, {t, -1, 1}, Frame -> True, PlotRange -> {{-1, 1}, {-1, 1}}],
{{a, 1, Style["Parameter 1", 10]}, -10, 10, 0.01},
{{b, 1, Style["Parameter 2", 10]}, -10, 10, 0.01, Enabled -> (a > 0)}
]


If you wanted more dependence on a, it is fairly easy to just use If statements in the body of your code:

Manipulate[
If[a < 0, b = 0];
Plot[a t^2 - b t, {t, -1, 1}, Frame -> True, PlotRange -> {{-1, 1}, {-1, 1}}],
{{a, 1, Style["Parameter 1", 10]}, -10, 10, 0.01},
{{b, 1, Style["Parameter 2", 10]}, -10, 10, 0.01, Enabled -> (a > 0)}
]

• Apparently, the Dynamic command need to be there for my code to properly work. Without it, the slider isn't grayed out. Strange, since your code DO work !
– Cham
Dec 21 '16 at 13:48