且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在PropertyGrid中设置ReadOnly属性会将所有属性设置为只读

更新时间:2023-01-31 20:30:20

尝试使用ReadOnly属性装饰所有的类属性:

Try decorating ALL of your class properties with the ReadOnly attribute:

<[ReadOnly](False)> _
Public Property SomeProperty As Boolean
  Get
    Return _someProperty
  End Get
  Set(value As Boolean)
    _someProperty = value
    If value Then
      SetReadOnlyProperty("SerialPortNum", True)
      SetReadOnlyProperty("IPAddress", False)
    Else
      SetReadOnlyProperty("SerialPortNum", False)
      SetReadOnlyProperty("IPAddress", True)
    End If
  End Set
End Property

<[ReadOnly](False)> _
Public Property IPAddress As String = "0.0.0.0"

<[ReadOnly](False)> _
Public Property SerialPortNum As Integer = 0

从以下代码项目中找到它:启用/禁用属性在运行时在PropertyGrid中

Found it from this Code Project: Enabling/disabling properties at runtime in the PropertyGrid

为了使所有这些正常工作,将类的每个属性的ReadOnly属性静态定义为所需的任何值都是很重要的.如果没有,那么在运行时以这种方式更改属性将错误地修改类的每个属性的属性.

In order for all this to work properly, it is important to statically define the ReadOnly attribute of every property of the class to whatever value you want. If not, changing the attribute at runtime that way will wrongly modify the attributes of every property of the class.