Search code examples
c#.netpropertygrid

PropertyGrid, DefaultValue, Unknown Colors


To Any...To All,

My property grid is inspecting a class that has several color properties...

The colors are not system colors, nor 'Known' colors...

When displaying the colors the text value in the grid might look like this:

209, 175, 171

How do I define [Attribute] the Property so that when this color is chosen, the PropertyGrid understands that the default color has been chosen?

I have tried:

[DefaultValue(typeof(Color),"209 , 175, 171")]
[DefaultValue(typeof(Color),"209,175,171")]

No luck so far...

Thanks for any help...

This site rocks...it has helped me more than any other site as I trudge through this project...

Carson


Solution

  • I just tried this in a Windows Forms app and it works fine. Here is my entire app:

    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e) {
            propertyGrid1.SelectedObject = new Foo();
        }
    }
    
    public class Foo {
        [DefaultValue("foo")]
        public string MyString { get; set; }
    
        [DefaultValue(typeof(Color), "209 , 175, 171")]
        public Color MyColor { get; set; }
    }
    

    And my form is a default form with a PropertyGrid control on it.

    When the color is set to 209,175,171 it shows in normal text. If I change any value it shows up as bold. Similarly, when the string is set to any text it's bold and when I set it to "foo" then it shows in normal text.

    With non-default values:

    alt text

    With default values:

    alt text