Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making the Visual Studio designer ignore a public property

I have a UserControl with a public property using the following attributes:

[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

I have tried deleting the owner form, re-creating a fresh form in Visual Studio 2010, and adding this UserControl to the form. It keeps adding a line like the following in the Designer file:

this.vMyUserControl.MyProperty = ((MyNamespace.MyClass)(resources.GetObject("vMyUserControl.MyProperty")));

This crashes my application because this property is not designed to be created by serialization.

like image 640
Trevor Elliott Avatar asked Oct 16 '11 06:10

Trevor Elliott


1 Answers

Making the property read only at design time will prevent it being serialized into the resx file. Strangely if MyType happens to be a collection the read only is ignored by the designer and you can still set the property at design time even though the property isn't written out into the resx so it's best to make the property not browsable too.

[ReadOnly(true)]
[Browsable(false)]
public MyType MyProperty
{
    get { return _MyProperty; }
    set { _MyProperty = value; }
}
like image 137
David Ewen Avatar answered Sep 22 '22 13:09

David Ewen