[.net] [Solved] Default Value for Icon Property

Started by
5 comments, last by nerd_boy 14 years, 10 months ago
First off, it'd be best if I stated that I could very well be overlooking the correct way to do this. As benryves'll attest, I managed to b0rk up using DefaultValue for a bit this morning. Anywho. I'd like to set the DefaultValue for three properties of typeof(Icon). The problem is that the way I thought it would be done: [DefaultValue(typeof(Icon),Resources.Icon1.ToString())] doesn't work. Turns out, unless I managed to bork the test up to, that Resources.Icon1.ToString() just returns "(Icon)". So is there anyway to set the string for the Icon in the Resources bit(I didn't see any, but figured I'd ask), or am I going about this the wrong way. Google isn't being overly helpful. I checked System.Windows.Forms.Form in Reflector, since it has an Icon property, but I didn't see any DefaultValue stuff for it, just AmbientValue, which didn't seem to be what I was looking for when I quicky looked it up on msdn. [SOLVED] Right, so apparently there is this neat little pattern matching thing that uses reflector that PropertyGrid, at least, uses. When the DefaulveValueAttribute just won't do the trick, having a function ShouldSerialize<insert property name>(value) that returns a bool as to whether or not the value is the same as the desired default value. [Edited by - nerd_boy on June 20, 2009 12:42:57 AM]
Advertisement
Why don't you try just "Resources.Icon1" instead of converting it to a string?
----------------------------------------MagosX.com
That doesn't compile. Then again, my 'example' doesn't compile either. Should be "Icon1", the resource name, instead of Resources.Icon1.String(). Either way, it still doesn't work/compile properly. :/
I don't think you should be using the from of the DefaultValue constructor that takes a type and a string if you want to specify an Icon. It is not clear from the docs how it converts the string to the type, but I think it is very unlikely that it will interpret the string as a resource reference. I am also pretty sure that taking the ToString() of an Icon will not give you its resource name.

Try doing as Magos suggests, but using the form of the constructor that takes a single object and specify the the static Icon value from the resource as the object.

[DefaultValue(Resources.Icon1)]

I don't know how the timing between Resources and Attribute compilation, but I suspect that this will be OK.


Hope this helps.
[DefaultValue(Resources.ExternalEditor)], copy/paste/verbatim, results in:

Error 2 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type C:\[rnfn]\Projects\rnfnCodeLite\rnfnCodeLite\rnfnCodeLite.cs 41 23 rnfnCodeLite
Bummer. You may not be able to reference a Resource via its static designer members in an attribute. You can try constructing an Icon using the type Icon constructor that takes a type and a resource string, but it is not clear to me that the type (the class you are compiling) that you need to pass the constructor will exist at the time attribute is evaluated. You will also need to figure out its resource name (it may be something like 'FullClassname.ResourceName.ico'). Look at the Icon docs here. This is about the limit of my knowledge on the subject.

What are you using the DefaultValues for? Are you making custom designer controls?
Right. I'll have a look see at the docs then. Thanks. And, yes, a custom code editor control.

This topic is closed to new replies.

Advertisement