LOGIN    REGISTER    YOUR CART

homepage
VisualHint's blog
2005
Oct22

How to manage the appearance of a default value

The Microsoft PropertyGrid has a built-in mechanism to handle default values: it displays them by using a non-bold font while the other properties are painted with the bold style. The Visual Studio designer also proposes a menu to reset a property to its default value.

I could have done the same in SPG but this would not be very flexible. Some people, like I did in a C++ application recently, will want for example to display a very subtle different background for non default values. Or maybe you simply want no difference at all. That's why I provide no built-in mechanism at all !!

Therefore I had to show you how simple this is to do it externally. Here is an example that will simply mimic the Microsoft way (the code is placed in a derived class of PropertyGrid):

protected void CheckDefaultValue(PropertyEnumerator propEnum)
{
    if (propEnum.Property.Value == null)
        return;

    DefaultValueAttribute attr = (DefaultValueAttribute)propEnum.
        Property.Value.GetAttribute(typeof(DefaultValueAttribute));

    if ((attr != null) && attr.Value.Equals(propEnum.Property.Value.GetValue()))
        propEnum.Property.Value.Font = this.Font;
    else
        propEnum.Property.Value.Font = new Font(propEnum.Property.
            Value.Font, FontStyle.Bold);
}

// Check the property value against the default value and set the
// font accordingly
protected override void OnPropertyChanged (VisualHint.SmartPropertyGrid.PropertyChangedEventArgs e)
{
    base.OnPropertyChanged(e);
    CheckDefaultValue(e.PropertyEnum);
}

// Set the correct font on the property when it is initially created
protected override void OnPropertyCreated(PropertyCreatedEventArgs e)
{
    base.OnPropertyCreated(e);
    CheckDefaultValue(e.PropertyEnum);
}