1

The following returns the user's choice among four items on a radio button bar, but it can also give a null return if they hit "OK" without selecting an option. How to avoid this? How to grey out the "OK" button (or there may be another way to achieve the same result) until a selection has been made?

a = DialogInput[{choice = {}}, 
Column[{RadioButtonBar[
  Dynamic[choice], {"One  ", "Two  ", "Three", "Four"}], 
 Button["OK", DialogReturn[choice]]}]];
ool
  • 45
  • 8
  • 2
    a = DialogInput[{choice = {}}, Column[{RadioButtonBar[Dynamic[choice], {"One ", "Two ", "Three", "Four"}], Button["OK", If[Not[choice === {}], DialogReturn[choice]]]}]] – Bob Hanlon Oct 01 '23 at 20:05
  • Thanks for this. It works a treat! I have no idea why === is needed, but it seems to be, given that == leaves the bar open regardless of whether or not an option is chosen before OK is clicked. – ool Oct 01 '23 at 21:38
  • @BobHanlon - But a user hell-bent on breaking something can still cancel the button window by clicking on X. Is there a way to prevent this? – ool Oct 01 '23 at 22:09
  • Clear[a]; While[Not[StringQ[a]], a = DialogInput[{choice = {}}, Column[{RadioButtonBar[Dynamic[choice], {"One", "Two", "Three", "Four"}], Button["OK", If[choice =!= {}, DialogReturn[choice]]]}]]] – Bob Hanlon Oct 02 '23 at 02:27

1 Answers1

1

To prevent a null return when the user hits the "OK" button without making a selection and to grey out the "OK" button until a selection has been made, you can use the following modified code:

 a = DialogInput[
      {
       choice = None,
       enabled = False
       },
      Column[{
        RadioButtonBar[Dynamic[choice], {"One  ", "Two  ", "Three", "Four"}],
        Button["OK", If[choice === None, Beep[], DialogReturn[choice]], 
         Enabled -> Dynamic[enabled]]
        }],
      NotebookEventActions -> {
        {"KeyDown", "Return"} :> If[choice === None, Beep[], DialogReturn[choice]]
        },
      Initialization :> (Dynamic[enabled] = choice === None)
      ]
zeraoulia rafik
  • 532
  • 2
  • 8
  • Many thanks for this, but I'm not getting it to work. It greys out "OK", but it leaves it greyed out after one of the buttons has been selected. – ool Oct 01 '23 at 21:37