开发者

How to add tooltip to enum selection in propertygrid in C#?

开发者 https://www.devze.com 2023-04-02 02:21 出处:网络
I just found out how to use the basic features of a PropertyGrid and I found that some of my enum constants aren\'t very self-explanatory. Is it possible that when the user o开发者_如何学Cpens the lis

I just found out how to use the basic features of a PropertyGrid and I found that some of my enum constants aren't very self-explanatory. Is it possible that when the user o开发者_如何学Cpens the list of all the enum constants that a tooltip will appear for whichever constant he hovers his mouse over?

For example if I have a property in a PropertyGrid called SomeEnum and the values are Enum1, Enum2, Enum3. When the user wants to change the value of the property, he brings down the list and hovers over Enum1, a tooltip will appear saying "This is Enum1" and so on.


You would want to override the default convert-to-string functionality of the enum as described in these S.O. posts:

C# String enums

C#: How to use a Type Converter to localize enums

or this MSDN article:

http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter%28VS.80%29.aspx


I don't believe there's any easy way to do what you're asking with a PropertyGrid. What you really should be doing is to rename your enumeration values to express their purposes more clearly; this has the added benefit of making your source code easier to understand.

If you need to specify additional information regarding a particular property that's being set through the PropertyGrid, you can do so by tagging it with a DescriptionAttribute:

[Description("This is my description of this property")]
public int Foo { get; set; }

If you're really dead set on keeping your enum values as they are, you could potentially implement a TypeConverter and flag each property that uses a particular enum with TypeConverterAttribute; this would let you explicitly specify a conversion between the enum values and the text that gets displayed in the PropertyGrid. See this MSDN article for more details.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号