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.

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.

share|improve this question
    
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. –  Kuba Sep 13 '13 at 6:38
    
@Kuba DynamicModule works. Although it works, I am just assuming that is the technique that is best advised. –  Liam William Sep 13 '13 at 13:50
    
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? –  Liam William Sep 13 '13 at 17:43
    
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. –  Kuba Sep 13 '13 at 19:05

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
3  
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]). –  John Fultz Sep 14 '13 at 23:09
    
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. –  John Fultz Sep 15 '13 at 21:54

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.