I have a list of variables (c1,c2,c3,c4), each with its own range of possible values and each being a possible control.

I need to pick two of them and make the remaining ones controls in a DynamicModule. I tried to use suggestions from here but I didn't mange to make the last step work.

DynamicModule[{allC, lC, fC, sC},
  allC = {{{c1, 1, "c1"}, {1, 2}}, {{c2, 10, "c2"}, {10, 20}}, 
          {{c3, 100, "c3"}, {100, 200}}, {{c4, 1000, "c4"}, {1000, 2000}}};
  lC = Range[Length[allC]];
  With[{controlGenerate = Function[{var, initialValue, str, range}, 
                                    Control[{{var, initialValue, str}, range}], HoldAll]},
    Column[{controlGenerate[fC, 1, "First", lC],
            controlGenerate[sC, 2, "Second", DeleteCases[lC, _?(MemberQ[{fC}, #] &)]],
            (* The line below works as I expected *)
            Dynamic@allC[[DeleteCases[lC,_?(MemberQ[{fC,sC},#] &)]]],
            (* The line below doesn't work as I intended *)
            (controlGenerate[#[[1, 1]], #[[1, 2]], #[[1, 3]], #[[2]]] & /@                  
               (allC[[DeleteCases[lC, _?(MemberQ[{fC, sC}, #] &)]]]))
  • $\begingroup$ Well, the easiest thing seems to use the in-place evaluation on your second line: (controlGenerate[...]&/@...) /. p_Part :> With[{eval = p}, eval /; True]. $\endgroup$ Sep 18, 2013 at 15:54
  • $\begingroup$ Do you want an improvement of your code or a solution that fits the description? $\endgroup$
    – Kuba
    Sep 18, 2013 at 16:04
  • $\begingroup$ @LeonidShifrin This returns controls on all 4 variables, not just on the two left after the selection. Also, it doesn't work if I re-evaluate the whole DynamicModule. $\endgroup$ Sep 18, 2013 at 18:32
  • $\begingroup$ @Kuba If you have a better way or a different approach, please do share ! $\endgroup$ Sep 18, 2013 at 18:32
  • $\begingroup$ @b.gatessucks Sorry, I did not look at this carefully enough. One thing I am sure of is that code generation is one of the best options for building Mathematica UI-s, since those are (possibly complex) Mathematica expressions. Alas, no time right now, but I might be able to revisit this one later. $\endgroup$ Sep 18, 2013 at 18:37

1 Answer 1


This is solution for Setter, or Slider, or any other that does not have HoldFirst attribute like Control.

Probably you can Block this attribute or deal with this using those replacements/evaluations in place, but I can't, I must start using those some time :)

Other important difference is that I'm not keepeing variable's names in the list. Those variables are just c[i] in order to handle them easily via Table/Array.

DynamicModule[{fC = 1, sC = 2, lC, controlGenerate},
       allC = {{1, 2}, {10, 20}, {100, 200}, {1000, 2000}};
       lC = Range[Length[allC]];

               Dynamic@controlGenerate[Dynamic@fC, lC],
               Dynamic@controlGenerate[Dynamic@sC, Complement[lC, {fC}]],

               Dynamic@Table[With[{i = i}, controlGenerate[Dynamic@c[i], allC[[i]]]], 
                             {i, Complement[lC, {fC, sC}]}],
               Table[With[{i = i}, Dynamic@c[i]], {i, 4}]
       Initialization :> (   
                SetAttributes[controlGenerate, HoldFirst];
                controlGenerate[var_, range_] := SetterBar[var, range];

enter image description here

  • there is Complement instead of DeleteCases in general it will not be the same but here I used it only for code transparency,
  • also for transparency I skipped labels, you can easily add them
  • Dynamic@controlGenerate <- Dynamic here probably could be skipped if you can initialize controlGenerate at start, but even that I've asked a question "What is the right way to construct DynamicModule" I'm still struggling with it often.

Looking forward to your comment.

  • $\begingroup$ I know, I know, the fun is with creating those controls with list you've provided. I think it is good for training but if it comes about creating applications I usually use buil-in solutions because it is possible to do what we want with them. Sometimes I just have to forget about habits or feelings about syntax and such cooperation works well :) $\endgroup$
    – Kuba
    Sep 18, 2013 at 20: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.