# Hide Text dynamically

I have a graphic, with on the left a PopupMenu, a Rectangular, a Text and a Button.

If the value of the PopupMenu is right/wrong, after pressing the button, the Rectangular become green/red and the Text become right/wrong.

Labeled[listar1 = {PopupMenu[Dynamic[r1], {"Sen","Cos","Tan"}]};
Plot[Sin[x],{x, -2 Pi, 2 Pi}, PlotStyle->Red,PlotRange->{-4,4},ImageSize->Large],
Column[Flatten[{{Text["What function?"],listar1},
Graphics[{Dynamic[If[r2,Green,Red]], Rectangle[{0, 0}]},ImageSize->{20,20}],
Graphics[Text[Style[Dynamic[If[r2,"Right!","Wrong!"]],Medium]],ImageSize->{100,30}],
Button["Check",r2=(r1==="Sen")]}]],Right];


How can I set the default color of the Rectangular (Blue for example)?

And also, how can I hide the Text at the beginning with no string showed?

• If takes a third argument for neither true nor false. Use that. e.g. If[r2, Green, Red, Blue] and If[r2, "Right!", "Wrong!", ""]. May 25 '17 at 16:59
• @MB1965 It doesn't work: the rectangular has still the last color and the text also. There is only a new empty entry in the popupmenu May 25 '17 at 17:04
• Where are you using that? Are you adding the third argument to the If calls you have? I should note it works for me (just tried it). May 25 '17 at 17:05
• @MB1965 Labeled[listar1 = {PopupMenu[Dynamic[r1], {"Sen","Cos","Tan"}]}; Plot[Sin[x],{x, -2 Pi, 2 Pi}, PlotStyle->Red,PlotRange->{-4,4},ImageSize->Large], Column[Flatten[{{Text["What function?"],listar1}, Graphics[{Dynamic[If[r2,Green,Red,Blue]], Rectangle[{0, 0}]},ImageSize->{20,20}], Graphics[Text[Style[Dynamic[If[r2,"Right!","Wrong!",""]],Medium]],ImageSize->{100,30}], Button["Check",r2=(r1==="Sen")]}]],Right]; May 25 '17 at 17:07
• Also if you're adding the text outside of the Graphics you don't need a Text and Graphics wrapper on it. May 25 '17 at 17:07

Give this a try. I wrapped it in DynamicModule to localize r1 and r2:

DynamicModule[{r1, r2},
Labeled[
listar1 = {PopupMenu[Dynamic[r1], {"Sen", "Cos", "Tan"}]};
Plot[Sin[x],
{x, -2 Pi, 2 Pi},
PlotStyle -> Red, PlotRange -> {-4, 4},
ImageSize -> Large
], Column[
Flatten[{{Text["What function?"], listar1},
Graphics[{Dynamic[If[r2, Green, Red, Blue]], Rectangle[{0, 0}]},
ImageSize -> {20, 20}],
Dynamic[If[r2, "Right!", "Wrong!", ""]],
Button["Check", r2 = (r1 === "Sen")]}]
],
Right]
]


Before:

After: