Friday, August 24, 2007

Custom Control, Visual Studio Designer and Properties

Recently I had to create a custom control and I was having problems with all properties getting serialized into the InitializeComponent part of the containing form. (It was causing the White Screen of Darn). Using the debugging techniques specified in http://therajahs.blogspot.com/2007/08/debbuging-white-screen-of-darn.html, I found out that the issue was caused because during serializing - the Get Accessors were being called on my control. These were to be called only during run-time and not design time. In addition the InDesignMode property was not returning the correct value (which can happen). By default all those properties that have a get and a set will automatically appear in the Property Browser. All these properties will also get serialized to the form's InitializeComponent method. One can use the following attributes on properties to control the design time appearance in the property browser of all these properties. Browsable: Determines whether the property is visible in the property browser Category: Tells the property browser in which group to include this property Description: Provides text for the property browser displays in its description bar DesignOnly: Specifies that the design-time value of this property is serialized to the form's resource file, and is typically used on design-time-only properties MergableProperty: Allows this property to be combined with properties from other objects when more than one are selected and edited ParenthesizePropertyName: Specifies whether this property should be surrounded by parentheses in the property browser ReadOnly: Specifies that this property cannot be edited in the property browser But almost none of the pages tell you how to tell Visual Designer that a certain property is not for display or designing the form and to leave those properties alone. (For a while I even thought of removing those Gets/Sets). Then DesignerSerializationVisibility came to my rescue. When ever you include your custom control in a form, the designer will automatically serialize the values for each of the properties that implement a getter and setter (In addition to showing these properties in the property browser). This will happen even to those properties that have their Browsable property set to false. I was having a really tough time working with this as some of my properties took custom types and called code that shouldnt be accessed during design time. So to stop the Visual Studio designer from serializing all property values, you can use the DesignerSerializationVisibility attribute (namespace:System.ComponentModel). If its value is DesignerSerializationVisibility.Hidden, then that property's value will not get serialized into the InitializeComponent part of the code. DesignerSerializationVisibilityAttribute specifies what part of a property should be serialized by the designer. This attribute takes one parameter, which is a DesignerSerializationVisibility enumeration member: Visible - specifies that the object should be serialized. Content - specifies that contents of the object should be serialized. Hidden- specifies that the object should not be serialized. More info: Writing Custom Designers for .NET Components(2001) http://msdn2.microsoft.com/en-us/library/ms973820.aspx Make Your Components Really RAD with Visual Studio .NET Property Browser(2002) http://msdn2.microsoft.com/en-us/library/Aa302334.aspx Custom Design-time Control Features in Visual Studio .NET (2003) http://msdn.microsoft.com/msdnmag/issues/03/12/CuttingEdge/(2003) Building Windows Forms Controls and Components with Rich Design-Time Features(2003) http://msdn.microsoft.com/msdnmag/issues/03/04/Design-TimeControls/default.aspx Building Windows Forms Controls and Components with Rich Design-Time Features, Part 2 (2003) http://msdn.microsoft.com/msdnmag/issues/03/05/Design-TimeControls/default.aspx

No comments:

Post a Comment

Remember, if you want me to respond to your comment, then you need to use a Google/OpenID account to leave the comment.