This project is read-only.

Can't subclass because of DefaultValueAttribute

Topics: User Forum
Jan 4, 2007 at 9:00 AM
I'm trying to subclass the GradientCaption control to set some custom values for properties (colors, etc.) in order to use the same 'custom' component across the whole application.
In this way, if I want to change the colors of all the captions in my app I can do it rapidly by changing the base class properties.
I created a derived class and set the custom properties I needed in the constructor, for example:

public class ImpCaption : GradientCaption
public ImpCaption()
this.BorderColor = new Ascend.BorderColor(System.Drawing.Color.Gray);
this.ForeColor = System.Drawing.Color.Black;
this.GradientHighColor = System.Drawing.Color.WhiteSmoke;
this.GradientLowColor = System.Drawing.Color.LightGray;
this.GradientMode = System.Drawing.Drawing2D.LinearGradientMode.Horizontal;
this.Location = new System.Drawing.Point(683, 352);
this.Name = "gradientCaption1";
this.Size = new System.Drawing.Size(84, 20);
this.TabIndex = 8;
this.Text = "00.000.000,00";
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

Unfortunately when I put the custom component in a form, using the visual editor, the code generator automatically sets all the properties (included the ones I have 'overridden') this way:

// lbl
this.lbl.BorderColor = new Ascend.BorderColor(System.Drawing.Color.Gray);
this.lbl.ForeColor = System.Drawing.Color.Black;
this.lbl.GradientHighColor = System.Drawing.Color.WhiteSmoke;
this.lbl.GradientLowColor = System.Drawing.Color.LightGray;
this.lbl.GradientMode = System.Drawing.Drawing2D.LinearGradientMode.Horizontal;
this.lbl.Location = new System.Drawing.Point(506, 352);
this.lbl.Name = "lbl";
this.lbl.Size = new System.Drawing.Size(89, 20);
this.lbl.TabIndex = 11;
this.lbl.Text = "impCaption4";
this.lbl.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

This is not the behaviour I expected, because this way, if I change my subclass, the other forms don't get updated. I expected to not find settings for BorderColor, ForeColor, etc. that I customized.
I suppose this has to do with the DefaultValueAttribute that is set for these properties, and even trying to add a ShouldSerialize.. method that returns always false in my subclass doesn't help.
To my understanding it's better to set dafault values in constructors, that should prevent generation of unwanted code.
Anyone has any idea?