PropertyGrid
컨트롤을 사용하여 클래스 속성을 편집하고 다른 속성 설정에 따라 특정 속성을 읽기 전용으로 설정하려고합니다.PropertyGrid의 ReadOnly 속성 설정 Readonly 모든 속성 설정
Imports System.ComponentModel
Imports System.Reflection
Public Class PropertyClass
Private _someProperty As Boolean = False
<DefaultValue(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
Public Property IPAddress As String = "0.0.0.0"
Public Property SerialPortNum As Integer = 0
Private Sub SetReadOnlyProperty(ByVal propertyName As String, ByVal readOnlyValue As Boolean)
Dim descriptor As PropertyDescriptor = TypeDescriptor.GetProperties(Me.GetType)(propertyName)
Dim attrib As ReadOnlyAttribute = CType(descriptor.Attributes(GetType(ReadOnlyAttribute)), ReadOnlyAttribute)
Dim isReadOnly As FieldInfo = attrib.GetType.GetField("isReadOnly", (BindingFlags.NonPublic Or BindingFlags.Instance))
isReadOnly.SetValue(attrib, readOnlyValue)
End Sub
End Class
이 내가 값을 편집하기 위해 사용하고있는 코드는 다음과 같습니다 :
이
내 클래스의 코드Dim c As New PropertyClass
PropertyGrid1.SelectedObject = c
문제는 그 나는 True
에 SomeProperty
, 아무것도 설정하지 않을 때 그런 다음 False
으로 다시 설정하면 모두 속성이 읽기 전용으로 설정됩니다. 누군가 내 코드에서 오류를 볼 수 있습니까? ReadOnly
속성을 가진 클래스의 모든 속성을 장식
Brilliant - Thanks Lars –