MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: RadioButton Question

  • To: mathgroup at
  • Subject: [mg91109] Re: RadioButton Question
  • From: Albert Retey <awnl at>
  • Date: Wed, 6 Aug 2008 05:06:53 -0400 (EDT)
  • References: <g791ks$9ot$>

Chris Degnen wrote:
> Hi, Can anyone suggest how I can get some radio buttons to run
> functions, rather than just set a variable?
> This is a statement to create two buttons that set a value:-
> RadioButtonBar[Dynamic[state], {"Existing", "New"}]
> The following code runs functions, but also shows the buttons in a
> list, along with some output.  Any suggestions how I can achieve
> this functionality but with only the buttons and labels showing, as
> above?
> dofunction1[] := x = 1;
> dofunction2[] := x = 2;
> Module[{state = "Existing"},
>  {RadioButtonBar[Dynamic[state], {"Existing", "New"}],
>   Dynamic[If[state == "Existing", dofunction1[], dofunction2[]]]}]

This is what the second argument of Dynamic can be used for:

DynamicModule[{state = "Existing"},
  Dynamic[state, (If[# == "Existing", dofunction1[], dofunction2[]];
     state = #) &], {"Existing", "New"}

I have also used DynamicModule instead of Module, which seems to make
more sense in this case (Module will have finished when you see the
output in the notebook, the local variable state will leak from that
module as something like state$145 which usually does no harm but is not
what Module is intended to do...)



  • Prev by Date: Re: Simplify
  • Next by Date: Re: Evaluate part of rhs of RuleDelayed
  • Previous by thread: Re: RadioButton Question
  • Next by thread: New spherical functions not efficient