How to show the constant for the property instead of value
Hello I am working in VB.Net 2010 framework 2.0. Suppose I declare a property :
Dim NewColor As Color = Color.FromArgb(150, 145, 145)
Private _myColor As Color = NewColor
Public Property MyColor() As Color
Get
Return _myColor
End Get
Set(ByVal value As Color)
_myColor = value
End Set
End Property
In the 开发者_开发百科form designer, the property "MyColor" will be seen the value as 150, 145, 145. I want to see this value as "NewColor" in form designer. This is the same like ControlDark, ActiveBorder etc. system colors. I want that instead of the color value, designer should show the variable name. The .Net framework also use the above implementation for System Colors and same i want to do.
Thanks for any reply in advance.
This is possible, but not easy.
You should define your own type, probably hide the current property in the browser, with a <Browsable(False)>
attribute, and create a shadow property that is of your own type. This type should know when it is "pointing to" a variable, or has a color itself.
On the new type you should override ToString, to return what you want to display. And create your own Editor. Look at EditorAttribute for more information.
You can create a dropdown, like Color has, with an extra tab that lists your variables.
If you do not want to create the extra properties (it is bad OO), you can also define a TypeConverter on the class, and specify each property and how to behave yourself.
BTW: The Color structure stores a KnownColor value (Color.Red or SystemColor.WindowText) or the RGB values. This way it knows that it points to a known color. Your structure should also know if it points to a variable (and what variable) or is a System.Color.
精彩评论