In the Property Sheet, if you change the value of a Property, you can then right-click
that Property and choose "Reset" to set it back to its Default Value. What code do you
need in your own controls to get that to work?
You need code in two places: in the Constructor to set the value to begin with,
and a DefaultValue attribute for the Property itself:
public class MyTextBox : TextBox
{
private string m_MyProperty;
public MyTextBox
{
this.m_MyProperty = "";
// As an additional bonus, I'm showing you two ways to do color
this.BackColor = System.Drawing.Color.FromArgb(90, 100, 240);
this.ForeColor = System.Drawing.Color.Firebrick; ;
}
[DefaultValue("")]
public string MyProperty
{
get {return this.m_MyProperty;}
set {this.m_MyProperty = value;}
}
[DefaultValue(typeof(System.Drawing.Color), "90,100,240")]
public override System.Drawing.Color BackColor
{
get
{
return base.BackColor;
}
set
{
base.BackColor = value;
}
}
[DefaultValue(typeof(System.Drawing.Color), "Firebrick")]
public override System.Drawing.Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
}
}
}
Setting some of these properties doesn't always work as you would expect
them to ... one that comes to mind is "Text" ... you may or may not have
problems setting a default for that one, depending on which control you're
sub-classing.
Also, some properties aren't virtual and can't be overridden. For
those properties, you can use "new" instead of "override".
from a solution provided by Bonnie Berent in Messages #1196770 and #1197504
|