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.

Consider the following snippet of code:

  Module[{pr, ps, var, option},
   option = 1;
   var = 0;
   pr[] := Module[{}, var++; Print[var]];
   ps[] := Print["Test..."];

   PaneSelector[
    {1 ->
      Button["Defaults 1",
      option = 2,
      ImageSize -> 100],
     2 ->
      Button["Defaults 2",
      option = 1;
      pr[],
      ImageSize -> 100]},
     Dynamic[option]]
  ]

Note that pr[] is executed when the button "Defaults 2" is clicked.

Question: I want to execute a function immediately after a Pane is selected in a PaneSelector construction. How should this be implemented?

( Question translated to the context of the above snippet: how to execute either ps[] or pr[] immediately after setting option to 1 or 2?)

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

The following seems to work fine:

DynamicModule[{pr, ps, var = 0, option = 1},
 pr[] := (var++; Print[var]);
 ps[] := Print["Test... Option value is ", option];
 PaneSelector[{1 -> 
    Button["Defaults 1", option = 2, ImageSize -> 100], 
   2 -> Button["Defaults 2", option = 1;
     pr[], ImageSize -> 100]}, 
  Dynamic[Refresh[ps[]; option, TrackedSymbols :> {option}]]]]

It executes ps[] every time option changes. Refresh with TrackedSymbols :> {option} is used to prevent infinite recursion. Note: I also changed Module to DynamicModule.

share|improve this answer
    
Interesting. I haven't used Refresh or TrackedSymbols before. Will try the code now. –  ndroock1 May 30 '12 at 19:36
    
It does work in a Module too. Is there a compelling reason to use DynamicModule ? –  ndroock1 May 30 '12 at 19:43
    
@ndroock1 There are several good reasons, documented here. –  Ajasja May 30 '12 at 19:56
add comment

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.