C# Enabling/Disabling fields in PropertyGrid -
i developing user control library, in need provide programmer, property grid customize properties of control.
if programmer uses system.windows.forms.propertygrid
(or visual studio's designer)
of property fields in system.windows.forms.propertygrid
should enabled/disabled depending on other property of same user control.
how it?
example scenario
this not actual example, illustration only.
ex: usercontrol1
has 2 custom properties:
myprop_caption: string
,
myprop_caption_visible: bool
now, myprop_caption should enabled in propertygrid when myprop_caption_visible true.
sample code usercontrol1
public class usercontrol1: usercontrol <br/> { public usercontrol1() { // skipping details // label1 system.windows.forms.label initializecomponent(); } [category("my prop"), browsable(true), description("get/set caption."), defaultvalue(typeof(string), "[set caption here]"), refreshproperties(refreshproperties.all), readonly(false)] public string myprop_caption { { return label1.text; } set { label1.text = value; } } [category("my prop"), browsable(true), description("show/hide caption."), defaultvalue(true)] public bool myprop_caption_visible { { return label1.visible; } set { label1.visible = value; // added solution: // additional stuff enable/disable // myprop_caption prop in propertygrid depending on value propertydescriptor propdescr = typedescriptor.getproperties(this.gettype())["myprop_caption"]; readonlyattribute attr = propdescr.attributes[typeof(readonlyattribute)] readonlyattribute; if (attr != null) { system.reflection.fieldinfo afield = attr.gettype().getfield("isreadonly", system.reflection.bindingflags.nonpublic | system.reflection.bindingflags.instance); afield.setvalue(attr, !label1.visible); } } } }
sample code propertygrid on usercontrol1
tstfrm simple form following 2 data members
private system.windows.forms.propertygrid propertygrid1; private usercontrol1 usercontrol11;
and can customize usercontrol1 through propertygrid1 below:
public partial class tstfrm : form { public tstfrm() { // tstfrm embeds propertygrid propertygrid1 initializecomponent(); propertygrid1.selectedobject = usercontrol11; } }
how enable/disable field myprop_caption in property grid depending on value of myprop_caption_visible?
(answered op in question edit. converted community wiki answer. see question no answers, issue solved in comments (or extended in chat) )
the op wrote:
solved!
thanks @simon mourier! posting edited code. result:
Comments
Post a Comment