LOGIN    REGISTER    YOUR CART

homepage
VisualHint's blog
2005
Dec05

The versatility of the Checkbox'ed property

When the user wants to select some values from a definite small number of choices, attaching a checkbox look/feel to the property is a good way to achieve exactly that, instead of using the usual combobox. Instinctively you may think that only an enumeration type can use such a feature. Actually, three types can.

Enumeration: this is the usual case. You may apply a custom attribute to display the enum values differently.

[Flags]
public enum SupportedOSs
{
    none = 0,
    win95 = 1,
    win98 = 2,
    winxp = 4
}

private SupportedOSs _oss;

[PropertyValueDisplayedAsAttribute(new string[] { "None", "Windows 95", "Windows 98", "Windows XP" })]
public Checklist SupportedOperatingSystems
{
    get { return _oss; }
    set { _oss = value; }
}

propEnum = AppendProperty(rootEnum, _id, "Supported OSs", this, "SupportedOperatingSystems", "");
propEnum.Property.Feel = GetRegisteredFeel(PropertyGrid.FeelCheckbox);
propEnum.Property.Value.Look = new PropertyCheckboxLook();
propEnum.Property.HeightMultiplier = propEnum.Property.Value.GetDisplayedValues().Length;

Boolean: a boolean can be attached to a single line checkbox. This is an extension to the above mechanism to have a convenient way to switch between yes/no, enabled/disabled or such states. This is far more convenient than the traditional combobox. Here again you can customize the displayed strings.

private bool _signalState = false;

[PropertyValueDisplayedAsAttribute(new string[2] { "Signaled", "Not signaled" })]
public bool SignalState
{
    get { return _signalState ; }
    set { _signalState = value; }
}

propEnum = AppendProperty(rootEnum, _id, "State", this, "SignalState", "");
propEnum.Property.Feel = GetRegisteredFeel(PropertyGrid.FeelCheckbox);
propEnum.Property.Value.Look = new PropertyCheckboxLook();

Collection: why to handle collections? Just because you may not know the set of possible values at compile-time. This gives something very easy to manage, like this:

private ArrayList _supportedOSs = new ArrayList();

public ArrayList SupportedOSs
{
    get { return _supportedOSs ; }
    set { _supportedOSs = value; }
}

List stdValues = new List();
stdValues.Add("Windows 95");
stdValues.Add("Windows 98");
stdValues.Add("Windows 2000");
stdValues.Add("Windows XP");
propEnum = AppendProperty(parentEnum, _id, "Supported OSs", this, "SupportedOSs", "",
       new PropertyValueDisplayedAsAttribute(stdValues));
propEnum.Property.Feel = GetRegisteredFeel(PropertyGrid.FeelCheckbox);
propEnum.Property.HeightMultiplier = stdValues.Count;
propEnum.Property.Value.Look = new PropertyCheckboxLook();