Ian Boyd
Ian Boyd

Reputation: 256671

How to tell if a WinForms control's property is the "default" value?

How can i tell if a property of a WinForms Control is the "default" value?

Note: At design-time you can tell that a property no longer is the "default" when it becomes bolded.

Here is an example (at design-time) of a label's font that is the "default" value (which means it gets its value from its parent):

enter image description here

(and if the parent font changes, this label's font changes)

Here a label's font has been changed, is no longer the default value:

enter image description here

(if the parent font changes, this label's font doesn't change)

A careful observer will note that these two labels on the same panel are the "same font"; except one is default and the other isn't.

Note: i need to know at runtime if a property is not the "default" value. (Someone might cheat and say, "Look at the properties window. If the property is bold you know it is no longer the default".)

Note: Note, my question is not limited to fonts of labels - that's just the example i used. Examples of other properties that i might want to know if it's still default:


My descent into madness attempts

i'll just leave these failed random rambling code vomit attempts here:

internal static Boolean GetIsPropertyDefault(object o, string propertyName)
{
    return false;

/*  Debug.WriteLine(String.Format("GetIsPropertyDefault: {0}.{1} = {2}", o, propertyName, TypeDescriptor.GetProperties(o)[propertyName].GetValue(o)));

    // Gets the attributes for the property.
    AttributeCollection attributes = TypeDescriptor.GetProperties(o)[propertyName].Attributes;

    DefaultValueAttribute myAttribute = (DefaultValueAttribute)attributes[typeof(DefaultValueAttribute)];

    Debug.WriteLine(String.Format("The default value is: {0}", myAttribute));

    if (myAttribute == null)
        return true;

    return (TypeDescriptor.GetProperties(o)[propertyName].GetValue(o) == myAttribute.Value);
*/

//          return (myAttribute.Value == null);



//          Console.WriteLine("The default value is: " + myAttribute.Value.ToString());

/*  System.Reflection.PropertyInfo pi;
    try
    {           
        //pi = o.GetType().GetProperty(propertyName).GetCustomAttributes(typeof(DefaultAttribute), false).
    }
    catch
    {
        return true;
    }
    if (pi == null) 
        return true;

    System.Diagnostics.Trace.TraceInformation(pi.Attributes.ToString());
    */

//          return false;
/*          foreach (Attribute attr in pi.Attributes)
    {
        if (attr is System.ComponentModel.DefaultValueAttribute)
        {
            System.ComponentModel.DefaultValueAttribute dv = (System.ComponentModel.DefaultValueAttribute)attr;

        }
    }
*/
}

Upvotes: 3

Views: 1511

Answers (1)

LarsTech
LarsTech

Reputation: 81610

With reflection, you can examine the attributes for the DefaultValue attribute. The PropertyGrid uses this to determine what should be bold and what shouldn't.

The "other" attribute to look for would be the AmbientValueAttribute.

When I did this:

List<string> list = new List<string>();
Attribute[] attrs = Attribute.GetCustomAttributes(typeof(Label).GetProperty("Font"));
foreach (Attribute att in attrs) {
  list.Add(att.ToString());
}

I got the following attributes:

  • System.ComponentModel.AmbientValueAttribute
  • System.Runtime.InteropServices.DispIdAttribute
  • System.Windows.Forms.SRCategoryAttribute
  • System.ComponentModel.LocalizableAttribute
  • System.Windows.Forms.SRDescriptionAttribute

From AmbientValueAttribute Class:

If a property on a control has ambient behavior, this attribute must be present. Ambient properties query their parent for their value, for example, a Control.Font property or a Control.BackColor property.

Typically, a visual designer uses the AmbientValueAttribute attribute to decide which value to persist for a property. This is usually a value that causes the property to get its value from another source. An example of an ambient value is Color.Empty as the ambient value for the BackColor property. If you have a control on a form and the BackColor property of the control is set to a different color than the BackColor property of the form, you can reset the BackColor property of the control to that of the form by setting the BackColor of the control to Color.Empty.

Probably doesn't make what you are trying to do any easier, since the AmbientValue basically tells the class to look at it's parent. For properties like "Font", you can just call the static function DefaultFont:

MessageBox.Show(Label.DefaultFont.ToString());

Which on my system, results in:

[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=0, GdiVerticalFont=False]

Upvotes: 4

Related Questions