This is an outgrowth of an earlier question.
I have an extensive user interface that has InputFields that I want to hold their specific structure as input by the user (e.g., keeping scientific notation if they enter the numbers that way). And since the code will be pretty much the same throughout for each of these (many) InputFields, I want a function to do it for me. I have what seems to be a great way to do this:
SetAttributes[HoldInputField, HoldFirst];
HoldInputField[var_, OptionsPattern[FieldSize -> 10]] :=
DynamicModule[{boxes = ToBoxes[var]},
InputField[Dynamic[boxes,
Function[result, boxes = result; var = ToExpression[boxes]]],
Boxes, FieldSize -> OptionValue[FieldSize]]]
We can test it like this:
test = 3;
Row[{HoldInputField[test], " = ", Dynamic[test]}]
Unfortunately, if the Row statement is itself within a Dynamic wrapper, the behavior fails:
Why do I need to do this? Because the number of fields itself is dynamic. For example:
numFields = 3;
fieldvalue[index_] = 0;
Column[{Row[{"Number of fields: ",SetterBar[Dynamic[numFields],Range[5]]}],
Panel[Dynamic[Column[
Row[{"Field ", #, ": ", HoldInputField[fieldvalue[#]],
" = ", fieldvalue[#]}] & /@ Range[numFields]]]]}]
Are there any ways to fix this problem? Thanks!
ToExpression
until you really need to. Otherwise you lose it and next time the field is called with a fraction value that contains no information about formatting. Shortly, your input fields should be a controller for a an array of boxes. You can use them however you need later. $\endgroup$MakeBoxes[var]
in place ofToBoxes[var]
? $\endgroup$