如何显示属性的常量而不是值

发布于 2024-10-07 11:52:53 字数 499 浏览 0 评论 0 原文

你好 我正在使用 VB.Net 2010 框架 2.0。 假设我声明一个属性:

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

在表单设计器中,属性“MyColor”将被视为值150、145、145。我想在表单设计器中将该值视为“NewColor”。 这与 ControlDark、ActiveBorder 等系统颜色相同。我希望设计者应该显示变量名称而不是颜色值。 .Net框架也使用上面的系统颜色实现,我也想做同样的事情。

感谢您提前回复。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪淡了忧伤 2024-10-14 11:52:53

这是可能的,但并不容易。

您应该定义自己的类型,可能使用 属性隐藏浏览器中的当前属性,并创建属于您自己类型的影子属性。该类型应该知道它何时“指向”变量,或者本身具有颜色。

在新类型上,您应该覆盖 ToString,以返回您想要显示的内容。并创建您自己的编辑器。有关详细信息,请参阅 EditorAttribute

您可以创建一个下拉列表,就像 Color 一样,带有一个列出变量的额外选项卡。

如果您不想创建额外的属性(这是不好的 OO),您还可以定义一个 TypeConverter 在类上,并指定每个属性以及您自己的行为方式。

顺便说一句:Color 结构存储 KnownColor 值(Color.Red 或 SystemColor.WindowText)或 RGB 值。这样它就知道它指向一个已知的颜色。您的结构还应该知道它是否指向变量(以及什么变量)或者是 System.Color。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文