3

I have a UserControl named "UserControl1" with a label inside it and a custom property:

[Browsable(true)]
public new string Text
{
    get { return label1.Text; }
    set { label1.Text = value; }
}

public UserControl1()
{
    InitializeComponent();
}

This UserControl is used in a form named "Form1". In the designer appears the property but when I write some text and build the application the text is cleared. I can see, the property isn't written in the Form1.Designer.cs.

If I change the property name to some other word all is ok.. Note the "new" keyword to override the base variable..

I have found a similar question here but there is no solution.

Greetings!

EDIT: There is no hardcoded value:

UserControl1.Designer.cs:

        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(64, 63);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(35, 13);
        this.label1.TabIndex = 0;
        // 
        // UserControl1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.Controls.Add(this.label1);
        this.Name = "UserControl1";
        this.Size = new System.Drawing.Size(181, 136);
        this.ResumeLayout(false);
        this.PerformLayout();

Form1.Designer.cx:

        // 
        // userControl11
        // 
        this.userControl11.Location = new System.Drawing.Point(35, 43);
        this.userControl11.Name = "userControl11";
        this.userControl11.Size = new System.Drawing.Size(181, 136);
        this.userControl11.TabIndex = 0;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(this.userControl11);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

To reproduce the problem just create a new windows form application, create an user control with a label inside it and with the property named "Text".

Community
  • 1
  • 1
elecyb
  • 33
  • 1
  • 5
  • Could you clarify this `...when I write some text and build the application the text is cleared...`? What is the code and where you place it in your class? – King King Jul 08 '13 at 03:10
  • Hi, I mean, when I write some string in the designer view.. It appears in the label (in the designer view), but when I run the application (build) the text disappears.. The corresponding code should appear in the Form1.Designer.cs.. something like this: this.userControl11.Text = "asdf"; But no.. – elecyb Jul 08 '13 at 22:30

2 Answers2

6

Try using override instead of new for the Text property and include the DesignerSerializationVisibility attribute:

[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text {
  get { return label1.Text; }
  set { label1.Text = value; }
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
1

Look inside InitializeComponent(). It is most likely hard-coding a default value when you place the control on the design surface.

apb
  • 413
  • 4
  • 8