views:

265

answers:

1

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

A: 

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

Eilon
That is bizarre...When you include the extra white space after the first value in the string it works....In other words this works:[DefaultValue(typeof(Color), "209 , 175, 171")] I would have never figured that out in a million years and went and made custom converter to deal with this....This does not:[DefaultValue(typeof(Color), "209, 175, 171")]I have no idea why nor do I really care...You answered and solved my problemCheers
Carson Wales
I am new around here....How do I mark your response as the answer?
Carson Wales
Can you try that for Color.Black (0,0,0)...it does not seem to work when you set the default value with this : [DefaultValue(typeof(Color), "0 , 0, 0")]....niether does this: [DefaultValue(typeof(Color), "Black")] ..... I think it has to do with the fact that Black is a "Known" Color...just a hunch
Carson Wales
For known colors you can also use [DefaultValue(typeof(Color), "Black")]. I tried various combinations of spaces and/or the textual names (including "Black" and "0,0,0") and it all worked.
Eilon
Its not working for either white or black for me...keep in mind that the default value is not an actual color but an int converted first via Color.FromARGB...which in turn is then converted again with the opacity or alpha at 255...in other words within the property I check if the var IsEmpty and if so do the double conversion to get the solid....EXAMPLE: _ColorSunDay = Color.FromArgb(255, Color.FromArgb(Default.COLOR_SUN_DAY));...where Default.COLOR_SUN_DAY = 16777215... I cannot control the form or type of value which is int in this case for the default....stuck with it...
Carson Wales