Home > View Post

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:

Conditional Buttons

The requirements are:
  1. If the radio button 'Apples' is selected then only the 'Add Apple' button should be enabled.
  2. If the radio button 'Pears' is selected then only the 'Add Pear' button should be enabled.
  3. 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;

Tags: C#

 
Josh Post By Josh Twist
12:14 AM
19 Jul 2006

» Next Post: Get organised with Solution Folders
« Previous Post: I heart PropertyGrid

Comments are closed for this post.

© 2005 - 2017 Josh Twist - All Rights Reserved.