Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UserControl Shows Property in Visual Studio Designer, But Property Is Not Changed

So today, I decided to create a custom TextButton control in C# using Visual Studio 2012. I quite like the design I've made, simple as it may be. However, when I decided I would move my completed component into a form using the Visual Studio Designer, I ran into a snag. Although the Text property shows up in the properties list, the property is not not actually being changed in the MainWindow.Designer.cs file. Below is the code that defines the property:

[Category("Appearance")]
[Description("The text displayed by the control.")] 
[Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
public override string Text
{
    get
    {
        return Button.Text;
    } 
    set
    {
        Button.Text = value;
    }
}

And here is the code that my MainWindow.Designer.cs file is creating in the InitializeComponent method:

this.Open = new UI.Controls.TextButton();
this.Open.Location = new System.Drawing.Point(9, 3);
this.Open.Name = "Open";
this.Open.Size = new System.Drawing.Size(112, 28);
this.Open.TabIndex = 4;
this.Open.Click += new System.EventHandler(this.OpenButton_Click);
like image 773
Noah Crowley Avatar asked Oct 04 '13 01:10

Noah Crowley


1 Answers

Part of the problem is that you are overriding the UserControl's Text Property. If you name your property another name besides Text such as myText it will work. In order to fix the problem you have using Text try adding the DesignerSerializationVisibility Attribute to your Property, that should take care of it.

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
like image 125
Mark Hall Avatar answered Oct 20 '22 12:10

Mark Hall