I am creating a custom label inheriting UserControl. To encapsulate Text property, I have created below script.
[Browsable(true)] // <--- This is necessary to show the property on design mode
public override string Text
{
get
{
return label1.Text;
}
set
{
label1.Text = value;
}
}
The only problem is that even though I set the Text property on designmode, when I rebuild the project, the text returns to default value.
public UCLabel() // <--- this is the class constructor
{
InitializeComponent();
BackColor = Global.GetColor(Global.UCLabelBackColor);
label1.ForeColor = Global.GetColor(Global.UCLabelForeColor);
label1.Text = this.Name;
}
What am I doing wrong here?
Obviously, the value of 'text' is not serialized.
To solve this problem you just have to add the DesignerSerializationVisibility Attribute
// This is necessary to show the property on design mode.
[Browsable(true)]
// This is necessary to serialize the value.
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get
{
return this.label1.Text;
}
set
{
this.label1.Text = value;
}
}