1
$\begingroup$

Consider the code

Manipulate[sli, Dynamic@Grid[{
    {"Consider option 3", Control[{{use, True, ""}, {True, False}}]},
    {"Options", 
     Control[{{sli, 1, ""}, {1 -> "Option 1", 2 -> "Option 2", 
        If[use, 3 -> "Option 3", Nothing]}, ControlType -> PopupMenu}]}},
   Alignment -> {{Right, Left}, Automatic}]]

enter image description here

Where Option 3 is only available if the first checkbox is true. Now, imagine I've selected option 3 and then uncheck the checkbox. I get

enter image description here

Option 3 remains selected, but no longer available on the popup menu. Is it possible to automatically pick any of the other options and maintain editability? That is, I could fix sli = 1 whenever the box is unchecked, but that forced me to stay in Option 1, as shown here

Manipulate[If[! use, sli = 1]; sli, Dynamic@Grid[{
    {"Consider option 3", Control[{{use, True, ""}, {True, False}}]},
    {"Options", 
     Control[{{sli, 1, ""}, {1 -> "Option 1", 2 -> "Option 2", 
        If[use, 3 -> "Option 3", Nothing]}, ControlType -> PopupMenu}]}},
   Alignment -> {{Right, Left}, Automatic}]]

enter image description here

Any ideas?

$\endgroup$
2
$\begingroup$

Change your logic to If[(! use) && prevUse, sli = 1]; prevUse = use;.

This will make sure you reset sli only on True -> False transition of use.

Manipulate[
 If[(! use) && prevUse, sli = 1]; prevUse = use;
 sli,
 Dynamic@Grid[{
    {"Consider option 3", Control[{{use, True, ""}, {True, False}}]},
    {"Options", Control[{{sli, 1, ""}, {
        1 -> "Option 1",
        2 -> "Option 2",
        If[use, 3 -> "Option 3", Nothing]
        }, ControlType -> PopupMenu}]}
    }, Alignment -> {{Right, Left}, Automatic}]]
$\endgroup$
2
  • $\begingroup$ But how do you make sli be either 1 or 2 (it can be randomly chosen) after you uncheck use while sli is set to 3? Basically I want to automatically update the value of sli to one of the two possible values (1 or 2) exclusively in the case where sli = 3 and I uncheck use. $\endgroup$
    – sam wolfe
    Jul 9 '21 at 13:45
  • $\begingroup$ Oh, okay, then the most straightforward way is to use If[(! use) && prevUse, sli = 1]; prevUse = use;. Will update the answer. $\endgroup$
    – Dan Oak
    Jul 9 '21 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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