1

我发现propertygrid 可以方便地在我的程序中编辑或显示一些自定义设置数据。但是我需要属性的某些属性是可变的。
像“只读”属性。

这是我到目前为止所拥有的:

Const myPersonCat As String = "MyPerson"
Const myDesc1 As String = "Firstname is one element"
<CategoryAttribute(myPersonCat), _
DescriptionAttribute(myDesc1), _
[ReadOnly](myBool)> _
Public Property firstname() As String
    Get
        Return _firstname
    End Get
    Set(ByVal value As String)
        If Not _firstname = value Then save_param("firstname", value, myPersonCat, myDesc1)
        _firstname = value
    End Set
End Property

Const mydesc2 As String = "but Lastname is second"
<CategoryAttribute(myPersonCat), _
DescriptionAttribute(mydesc2), _
[ReadOnly](myBool)> _
Public Property lastname() As String
    Get
        Return _lastname
    End Get
    Set(ByVal value As String)
        If Not _lastname = value Then save_param("lastname", value, myPersonCat, myDesc2)
        _lastname = value
    End Set
End Property

Save_param 是调用将属性与基本数据一起保存在数据库中的函数。
所有这些工作都很好。

但现在是一个问题......这里有一些不太复杂的方法来为只读属性设置'myBool',使用变量而不是常量,我可以阻止更改一些取决于程序情况的属性。
也许适用于整个类别或单个属性?

或者也许这里存在一些其他方式来获得类似的功能?

4

1 回答 1

0

不,没有办法改变属性的值。作为替代方案,您可以在 Property Set 中编写代码,使其在用户尝试设置该值时抛出异常,而该值应该是只读的。

于 2012-12-20T11:33:31.287 回答