Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Subclassing a subclass
Message
From
07/10/2007 11:52:36
 
 
To
07/10/2007 09:01:00
Alan Harris-Reid
Baseline Data Services
Devon, United Kingdom
General information
Forum:
ASP.NET
Category:
Class design
Miscellaneous
Thread ID:
01256172
Message ID:
01259255
Views:
20
Alan,

I should elaborate just a bit on my reply. An additional thing that you should add to your properties (for example, a BackColor property) is a DefaultValue attribute. If you don't do that, then when you drop the control on the design surface, the IDE will generate a line in the code to specify the BackColor setting that you set in the constructor of the Control. If you decided to change that to something else in your base class, you will unfortunately have to go back and touch everywhere you dropped that control to reset it. Not good.

So, here's how you avoid that:
public class MyTextBox : System.Windows.Forms.TextBox 
{
	public MyTextBox()
	{
		this.BackColor = Color.Firebrick; ;
	}
	[DefaultValue(typeof(System.Drawing.Color), "Firebrick")]
	public override System.Drawing.Color BackColor
	{
		get { return base.BackColor; }
		set { base.BackColor= value; }
	}
}
With the DefaultValue attribute in place, when this TextBox is dropped on a design surface, the line of code:
this.BackColor = Color.FireBrick;
will *NOT* be generated. Without the DefaultValue attribute, it *WILL* be generated.

~~Bonnie






>Hi Bonnie,
>
>>No, this particular problem that you had with the .Text property of a control is kind of special and that's because the IDE sets it when you drop the control on a design surface. You wouldn't normally handle it this way for other properties. Normally, all you need to do is set it in the constructor.
>
>I understand now. Thanks for the explanation.
>
>Alan
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform