Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to make a simple demonstration using Manipulate in which I have one or two input fields show depending on whether or not another control is True or False. How can I achieve such functionality?

Manipulate["DoSomethig", {ShowMore, {True, False}}, WhatShouldBeHere?]

ShowMore is unchecked ShowMore is checked

share|improve this question
1  
Possible duplicate of [this question](mathematica.stackexchange.com/q/30003/30660 –  m_goldberg Oct 13 '13 at 10:09

4 Answers 4

up vote 7 down vote accepted

You can achieve this functionality by

Manipulate[{a, b, c}, Dynamic@Column@{
    Control[{a, 0}],
    Control[{ShowMore, {False, True}}],
    Sequence @@ If[ShowMore,
      {Control[{b, 0}],
       Control[{c, 0}]},
      {}]
    }]

enter image description here

enter image description here

This is similar to the link provided by m_goldberg, but it has a more compact form more suitable for this question.

share|improve this answer
    
Very embarassing, but would you know where to place your Sequence @@ in my answer ? I couldn't manage. –  b.gatessucks Oct 13 '13 at 11:24
    
@b.gatessucks In your case it is much simpler to change Dynamic@If to Dynamic@Column@If. –  ybeltukov Oct 13 '13 at 11:52

b.gatessucks was faster than I, so he already wrote about DynamicModule. Again, the answer you're looking for might very well be found in this thread. ybeltukov's answer is excellent.

I want to publish this anyway as a boilerplate for people who want to go down the DynamicModule path. There are a few thing you have to do to mimic manipulate, like alignment, ContinousAction and using Grid rather than some other constructs that you'd think would work but which will in fact cause trouble, that are non-trivial.

Deploy@DynamicModule[{showMore = False, a = 1, b = 1},
  Panel[
   Column[{
     Dynamic@Grid[{
        {"Show more", Checkbox[Dynamic[showMore]]},
        {"a =", 
         InputField[Dynamic[a], Number, ContinuousAction -> True]},
        If[showMore,
         {"b =", 
          InputField[Dynamic[b], Number, ContinuousAction -> True]}, {}
         ]
        }, Alignment -> Left],
     Dynamic@Pane[
       Plot[a Sin[b x], {x, 0, 2 Pi}], 
       BaseStyle -> {Background -> White}
       ]
     }]]]

screenshot

share|improve this answer

You can find good suggestions using Manipulate in the link provided by @m_goldberg. An alternative, also mentioned there, is to use DynamicModule; below is a starting point.

    DynamicModule[{showMore, a, b , c},
     Panel@With[{controlGenerate = Function[{var, initialValue, str, range, controlType}, 
                 Control[{{var, initialValue, str}, range, ControlType -> controlType}], HoldAll]},
      Column[{controlGenerate[showMore, False, "Show more", {False, True}, Checkbox], 
        Dynamic@If[showMore, 
                   {controlGenerate[b, Pi, "b", {}, InputField], 
                    controlGenerate[c, E, "c", {}, InputField]},  
                   {controlGenerate[a, Sqrt[2], "a", {}, InputField]}], 
        Dynamic@Sin[a], 
        Dynamic@Cos[b + c]}]
   ]
 ]

screenshot

share|improve this answer

This is more of a comment on some of the other answers.

The problem with using If[test, display, (* else *) {}] type usage is that on its own it can lead to a lot of empty rows -- which we can see in other answers if a frame is added. e.g

enter image description here

So the way to fix this in your "openers" is to use Join since

Join[{{1, 2, 3}}, {}]
(* {{1, 2, 3}} *)

In other words Join removes the empty row giving you better design control of your layout.

enter image description here

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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