3
$\begingroup$

Does someone mind explaining why Module doesn't seem to work while inside ToBoxes or MakeBoxes.

SetAttributes[AutoInputField, HoldAllComplete];
AutoInputField /: MakeBoxes[AutoInputField[
    list_: {}, displayNum_: 3, imageSize_: 100], form_] :=
  ToBoxes@Module[{x = "default"},
    x = "default";
    Column[{
      InputField[Dynamic@x, String, ContinuousAction -> True, 
       ImageSize -> imageSize],
      Dynamic@x
      }
     ]
    ];
AutoInputField[]
AutoInputField[]

Ideally x should be set to "default", but as of right now x isn't taking on any value. Surprisingly Module is successful at scoping the different value because the two InputFields values don't change interactively.

I am looking for a way to modify/set the value of x inside the Module.

$\endgroup$
4
  • $\begingroup$ Someone has point this out to you in one of the previous question but let me quote John Fultz: "Module variables should never appear inside Dynamics or Manipulates internal to that Module.". That's ofcourse a side note. $\endgroup$
    – Kuba
    Sep 13 '13 at 6:38
  • $\begingroup$ @Kuba DynamicModule works. Although it works, I am just assuming that is the technique that is best advised. $\endgroup$
    – William
    Sep 13 '13 at 13:50
  • $\begingroup$ I have found the link that you are referring to here Quit useful. What do you think would be most appropriate: you posting a response, me posting a response, or closing/deleting the question? $\endgroup$
    – William
    Sep 13 '13 at 17:43
  • $\begingroup$ Remark about DynamicModule was only a side not. I must admit my I have not focus well on your question so I'm not going to answer :) If you've solved your issue it is encouraged to self-answer. $\endgroup$
    – Kuba
    Sep 13 '13 at 19:05
3
$\begingroup$

It appears using DynamicModule solves the issue quite well.

SetAttributes[AutoInputField, HoldAllComplete];
AutoInputField /: MakeBoxes[AutoInputField[
    list_: {}, displayNum_: 3, imageSize_: 100], form_] :=
  ToBoxes@DynamicModule[{x = "default"},
    x = "default";
    Column[{
      InputField[Dynamic@x, String, ContinuousAction -> True, 
       ImageSize -> imageSize],
      Dynamic@x
      }
     ]
    ];
AutoInputField[]
AutoInputField[]

Although John Fultz's answer here was not about Module values it appears his rules there hold true for this question also.

$\endgroup$
2
  • 3
    $\begingroup$ This is exactly what DynamicModule was designed to do, and it is exactly what I've warned against where Module is concerned. In 9.0.1, the code in your question would have colored red to warn you, except for a bug that causes it to not color for Dynamic@x (as opposed to Dynamic[x]). $\endgroup$
    – John Fultz
    Sep 14 '13 at 23:09
  • 1
    $\begingroup$ Incidentally, I've fixed the lack of proper syntax highlighting on Dynamic@x. In my development version, the non-DynamicModule version now highlights the x red in all appropriate places. $\endgroup$
    – John Fultz
    Sep 15 '13 at 21:54

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.