How can I get Text property to initialize to the objects name at design time?
When you add a label to the form from the toolbox, its text defaults to the item's name (label1
, label2
, etc). How can I make this happen with a custom control? So far, I have the following, which allows me to change the text through the property window:
private string _text;
[BrowsableAttribute(true)]
public override string Text
{
get { return _text; }
set
{
_text = value;
lblID.Text = _text;
}
}
Apparently the ab开发者_StackOverflow中文版ove code works as is, but I'm not sure why. Does Text
default to the object's name automatically? The question still stands for other properties which don't override Text
.
private string _text = "default value"
Look into System.ComponentModel.DefaultValueAttribute
Apparently the Text
property is automatically set to the objects name when you inherit from UserControl
. The following code works:
public partial class CustomControl: UserControl
{
public string Extension { get; set; }
private string _text;
[BrowsableAttribute(true)] // Initializes to "customControlN"
public override string Text
{
get { return _text; }
set { _text = value; }
}
}
精彩评论