Elegant conditionals in your UI
This post is applicable to lots of places other than UIs but that's where I use this style of writing most. Take this design:

The requirements are:
- If the radio button 'Apples' is selected then only the 'Add Apple' button should be enabled.
- If the radio button 'Pears' is selected then only the 'Add Pear' button should be enabled.
- If the radio button 'IFruit' is selected then both buttons should be enabled.
We could easily achieve this as follows:
if (_rdoApples.Checked)
{
_btnAddApple.Enabled = true;
_btnAddPear.Enabled = false;
}
else if (_rdoPears.Checked)
{
_btnAddApple.Enabled = false;
_btnAddPear.Enabled = true;
}
else
{
_btnAddApple.Enabled = true;
_btnAddPear.Enabled = true;
}
But this is much nicer:
_btnAddApple.Enabled = _rdoApples.Checked || _rdoIFruit.Checked;
_btnAddPear.Enabled = _rdoPears.Checked || _rdoIFruit.Checked;

Post By
Josh Twist
00:14
19 Jul 2006
» Next Post:
Get organised with Solution Folders
« Previous Post:
I heart PropertyGrid
Comments are closed for this post.