Message Boards Message Boards

Make a list of checkboxes that exclude each other?

Posted 7 years ago

I am looking for a simple way to make a list of checkboxes that exclude each other. Suppose one has the following checkboxes:

Checkbox[Dynamic[p]]
Checkbox[Dynamic[q]]
Checkbox[Dynamic[r]]

How do I make that if I check one of them the other two are unchecked. Sothat always only p or q or r is true, and the other two false. Thanks in advance for your help.

POSTED BY: Laurens Wachters
6 Replies

Thanks a lot Neil and Kuba for your advice. Your code works perfectly.

POSTED BY: Laurens Wachters

You can simplify Kuba's clever approach a bit by combining the set operation of the Dynamic (triggered by the Automatic) with the logic table you want (I think it makes the code a bit easier to read).

    {RadioButton[Dynamic[pp, ({pp, qq, rr} = {#, False, False}) &], True],
      RadioButton[Dynamic[qq, ({pp, qq, rr} = {False, #, False}) &], True], 
      RadioButton[Dynamic[rr, ({pp, qq, rr} = {False, False, #}) &], True], 
     Dynamic[pp], Dynamic[qq], Dynamic[rr]}

Alternatively (again building on Kuba's post) you can keep the radio button variable separate (which may be even easier to read and see what is going on but involves more code)

{RadioButton[Dynamic[x,({x,pp,qq,rr}={#,True,False,False})&],1],
RadioButton[Dynamic[x,({x,pp,qq,rr}={#,False,True,False})&],2],
RadioButton[Dynamic[x,({x,pp,qq,rr}={#,False,False,True})&],3],
Dynamic[pp],Dynamic[qq],Dynamic[rr], Dynamic[x]}

Also you can do something similar with a checkbox since you are now manually controlling the behavior anyway (so there is no need to rely on RadioButton)

{Checkbox[Dynamic[pp, ({pp, qq, rr} = {#, False, False}) &]],
 Checkbox[Dynamic[qq, ({pp, qq, rr} = {False, #, False}) &]],
 Checkbox[Dynamic[rr, ({pp, qq, rr} = {False, False, #}) &]],
 Dynamic[pp], Dynamic[qq], Dynamic[rr]}

Hope this helps.

Regards

POSTED BY: Neil Singer

See ref/Dynamic, the second argument of this expression is very useful:

Column @ {
    RadioButton[ Dynamic[p, {({p, q, r} = {False, False, False}) &, Automatic, None}], True]
  , RadioButton[ Dynamic[q, {({p, q, r} = {False, False, False}) &, Automatic, None}], True]
  , RadioButton[ Dynamic[r, {({p, q, r} = {False, False, False}) &, Automatic,  None}], True]
}

What can be shortened to:

event = {({p, q, r} = {False, False, False}) &, Automatic, None};

Column @ {
    RadioButton[Dynamic[p, event], True]
  , RadioButton[Dynamic[q, event], True]
  , RadioButton[Dynamic[r, event], True]
}
POSTED BY: Kuba Podkalicki

Thank you Neil. With the radio buttons one get values instead of true or false though. Would there be an easy way to convert the resulting values to True or False? What I mean is that when having e.g. three quantities p, q and r, corresponding with 3 radio buttons, by pushing the first radio button the quantity p becomes true and the other two false, etc.

POSTED BY: Laurens Wachters

The easiest is to use RadioButton:

{RadioButton[Dynamic[x], 1], RadioButton[Dynamic[x], 2], 
 RadioButton[Dynamic[x], 3], Dynamic[x]}

X will hold either 1,2,or3 depending on what button you push.

POSTED BY: Neil Singer

I should have added to the above post of mine that there is the possibility of using ChoiceDialog, e.g. ChoiceDialog [ "Pick one", {1 -> "a", 2 -> "b", 3 -> "c", 4 -> "d"}], shown under "Basic examples", but there the checkboxes are placed in a row. I want them placed in a column, and moreover be able to put text in between. I have experimented with the ChoiceDialog command, but I did not get quite far with it. So I think I should go out from a series of the much simpler command Checkbox[ ].

POSTED BY: Laurens Wachters
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract