1
$\begingroup$

I am creating a toolbar. One of the buttons on my toolbar is this setcounter button below.

setcounter = Dynamic[Button["Set Counters", {


    SetOptions[InputNotebook[], 
     CounterAssignments -> {{"StaticChapter", 3}, {"StaticSection", 
        5}}]},

   Appearance -> "Palette"]]

As shown, this button sets each counter to 3 and 5 respectively. I would like to create a prompt in the form of a popup window or dialog that will allow the user to enter their own positive integers. And then the option be set based upon those numbers.

I know I probably just need one line of code before "SetOptions..." but I can't seem to get anything to work. I've tried dialogs... but I can't get it to wait for the inputs before setting the option. Any Ideas?

$\endgroup$
4
$\begingroup$
DynamicModule[{counters, oldcounters},

 Button["Set Counters",
  oldcounters = 
   CurrentValue[InputNotebook[], {CounterAssignments}][[All, 2]];

  counters = DialogInput[{x = 0, y = 0},
    Column[{
      "SetCounters:",
      Row[{"StaticChapter ", InputField[Dynamic[x,(x = Round[#]) &], Number]}],
      Row[{"StaticSection ", InputField[Dynamic[y,(y = Round[#]) &], Number]}], 
      Row[{CancelButton[DialogReturn[$Canceled]], 
        Button["Reset", DialogReturn[{x, y}], ImageSize -> Automatic]}]
      }]
    ];
  If[counters =!= $Failed && counters =!= $Canceled, 
   SetOptions[InputNotebook[], 
    CounterAssignments -> {{"StaticChapter", 
       counters[[1]]}, {"StaticSection", counters[[2]]}}],

   SetOptions[InputNotebook[], 
    CounterAssignments -> {{"StaticChapter", 
       oldcounters[[1]]}, {"StaticSection", oldcounters[[2]]}}]
   ], Appearance -> "Palette", Method -> "Queued"]
 ]

enter image description here

$\endgroup$
  • $\begingroup$ Thank you Mike. This works great! I was thinking their would be a simpler solution but based upon what you've done maybe not? $\endgroup$ – Michael McCain Dec 24 '15 at 2:39
  • $\begingroup$ Spoke to soon.... Something isn't working with your code. If I try calling one of the counterboxs it doesn't work. I'm thinking it might be because your counters are stored as string and not numbers? $\endgroup$ – Michael McCain Dec 24 '15 at 2:41
  • $\begingroup$ Yep! Change both occurrences of String to Number in your code and it works perfect! Thank you! $\endgroup$ – Michael McCain Dec 24 '15 at 2:45
  • 1
    $\begingroup$ @MichaelMcCain yes correct. Also you need integers so I added a bit more to ensure that. $\endgroup$ – Mike Honeychurch Dec 24 '15 at 3:41

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.