Exclusive Checkboxes
Wednesday, April 30th, 2008Problem: Provide the user with a choice of options, limited to only one selection and allow them to deselect a selection.
When selecting the control style for a field, you have the choice of radio buttons and check boxes. Both are based on a value list. A radio button set gives the user an exclusive choice – if they select an second choice, the first is turned off. A checkbox set gives the user the ability to select multiple items.
One problem users sometimes have with radio buttons is accidental selection. Perhaps you make a selection and you want to 'unmake' the selection. A radio button can be turned off if you hold down the Shift key - but most users don't know that. And it is not something you should have to teach. You can't use checkboxes because you want an exclusive choice – only one item selected.
So how can you combine the usefulness of an exclusive choice provided by radio buttons with the ability to turn them off provided by checkboxes? I'm glad you asked.
Firstly, use a checkbox for the control style. Then set the auto-enter options for the field. Choose to auto-enter a calculated value using the expression:
GetValue ( yourfield ; ValueCount ( yourfield ) )
If you are using FileMaker Pro 9 and want to apply this to a number of fields, you can take advantage of the new Self function. The following expression can be copied and pasted for any field:
GetValue ( Self ; ValueCount ( Self ) )
When exiting the field options dialog, make sure "Do not replace existing value of field (if any)" is unchecked.
Now when you use the checkbox, you will have an exclusive choice enforced like a radio button set. And you have the added advantage of being able to turn off a checked field.
If you don't want or need to know how it works, you can stop reading. If you are curious or just need to know, read on!
One of the keys to this technique is knowing how checkbox fields work behind the scenes. When you select multiple items, they are stored as a return-separated list in the order in which they were selected. So the last item selected is the last item in the field list. You can see this if you have two copies of a field on a layout – one using checkboxes, the other a plain edit box.
In our scenario, we don't want to allow multiple selections – when a user selects a second item from the checkbox list, you want only that item to be retained. The auto-entry calculation above returns only the last (most recently selected) value in the field. It does this with two functions.
The first is the GetValue function. The syntax is GetValue ( listOfValues ; valueNumber ). In the calculation, you are using the current field (or Self) to supply the listOfValues. You want the valueNumber to be the number of the last value in the field. The number of the last value is calculated with the ValueCount function – the number of values is the number of the last value!
In summary, the expression says "give me back only the last value in the field". And that works for you because the last value is the most recently selected value. If the last remaining value is deselected, the field is empty and remains so.
Challenge: This technique can be extended to allow a maximum number of values to be selected from a checkbox list. For example, select your three favourite colours from a list. When you select a fourth, the first selected will be removed.




