Problem
I was trying to create a custom control which contains nothing but a label. However, I wanted the label's text to be changed to what the name property of the custom control has received at design time.
This is what my custom control's class looks like:
public partial class Tile : UserControl
{
public Tile()
{
InitializeComponent();
}
[Browsable(true)]
public override string Text { get => label1.Text; set => label1.Text = value; }
}
As you can see, I've overridden the UserControl's Text
property in a way that it updates the Label's text too as soon as the Name
is updated which in the end did not work. What happened was when I dragged the control from Toolbox to form the label got updated as expected but the moment I build the project, the designer got refreshed and the Label's text was lost.
What I tried
DesignerSerializationAttribute
Going through Google I came upon a solution given at StackOverflow itself by Hans Passant that using
DesignerSerializationAttribute(DesignerSerializationVisibility.Visible)
can solve the problem as the value theText
property is given will be persisted in the initialization code which seems valid when it was getting lost at first (value didn't persisted and lost upon designer repaint).So I changed my property like this:
[Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public override string Text { get => label1.Text; set => label1.Text = value; }
Doing such change actually solved the issue and now it was working even If I build the project.
What I found
Though my issue got solved but then I started to look for another way to approach the same result.
While experimenting more with my code I found that If I create
another property that exposes the label's Text
property and update
it using the overridden Text
property it works exactly what it
was working using DesignerSerializationAttribute
.
Here what the new code looks like:
[Browsable(false)]
public string LabelText { get => label1.Text; set => label1.Text = value; }
[Browsable(true)]
public override string Text { get => LabelText; set => LabelText = value; }
I wanted to know that
Why this works (even without DesignerSerializationVisibility
):
Text
---->LabelText
---->Label's Text
I might be asking something very obvious right now but I've been reading about it since hours which made it a bit confusing for me .