.net - Setting ReadOnly Property in PropertyGrid Sets All Properties Readonly -
i using propertygrid
control edit class properties , trying set properties read-only depending on other property settings.
this code of class:
imports system.componentmodel imports system.reflection public class propertyclass private _someproperty boolean = false <defaultvalue(false)> public property someproperty boolean return _someproperty end set(value boolean) _someproperty = value if value setreadonlyproperty("serialportnum", true) setreadonlyproperty("ipaddress", false) else setreadonlyproperty("serialportnum", false) setreadonlyproperty("ipaddress", true) end if end set end property public property ipaddress string = "0.0.0.0" public property serialportnum integer = 0 private sub setreadonlyproperty(byval propertyname string, byval readonlyvalue boolean) dim descriptor propertydescriptor = typedescriptor.getproperties(me.gettype)(propertyname) dim attrib readonlyattribute = ctype(descriptor.attributes(gettype(readonlyattribute)), readonlyattribute) dim isreadonly fieldinfo = attrib.gettype.getfield("isreadonly", (bindingflags.nonpublic or bindingflags.instance)) isreadonly.setvalue(attrib, readonlyvalue) end sub end class
this code using edit values:
dim c new propertyclass propertygrid1.selectedobject = c
the problem when set someproperty
true
, nothing happens , when set false
again sets all properties read-only. can see error in code?
try decorating of class properties readonly
attribute:
<[readonly](false)> _ public property someproperty boolean return _someproperty end set(value boolean) _someproperty = value if value setreadonlyproperty("serialportnum", true) setreadonlyproperty("ipaddress", false) else setreadonlyproperty("serialportnum", false) setreadonlyproperty("ipaddress", true) end if end set end property <[readonly](false)> _ public property ipaddress string = "0.0.0.0" <[readonly](false)> _ public property serialportnum integer = 0
found code project: enabling/disabling properties @ runtime in propertygrid
in order work properly, important statically define readonly attribute of every property of class whatever value want. if not, changing attribute @ runtime way wrongly modify attributes of every property of class.
Comments
Post a Comment