26

I have a control with a inner TextBox. I want to make a direct relationship between the Text property of the UserControl and the Text property of the TextBox. The first thing I realized is that Text was not being displayed in the Properties of the UserControl. Then I added the Browsable(true) attribute.

[Browsable(true)]
public override string Text
{
    get
    {
        return m_textBox.Text;
    }

    set
    {
        m_textBox.Text = value;
    }
}

Now, the text will be shown for a while, but then is deleted. This is because the information is not written automatically within the xxxx.Designer.cs file. How can this behviour be changed?

yeyeyerman
  • 6,983
  • 6
  • 41
  • 52

3 Answers3

49

You need more attributes:

[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Bindable(true)]
public override string Text { get; set; }
Andrei Krasutski
  • 3,487
  • 1
  • 21
  • 33
Hans Olsson
  • 51,774
  • 14
  • 88
  • 111
  • Don't forget to rebuild after adding the attributes for the changes to appear in the designer. – Bip901 Oct 25 '20 at 07:45
15

Reflector is a crucial tool for a .NET developer. It is immediately obvious what you need to do when you use it to look at the UserControl.Text property:

[Bindable(false), EditorBrowsable(EditorBrowsableState.Never), Browsable(false),
 DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override string Text
{
    get
    {
        return base.Text;
    }
    set
    {
        base.Text = value;
    }
}

Ho showed you what you need to do to cancel these attributes, too bad he didn't show you how he found out. Reflector is was free, download it from redgate.com or check the alternatives here : Something Better than .NET Reflector?

Community
  • 1
  • 1
Hans Passant
  • 873,011
  • 131
  • 1,552
  • 2,371
  • Hans, +1 for the advice. However I have to accept Ho answer, as it is also correct and it was written sooner. Thanks! – yeyeyerman May 24 '10 at 07:15
0

For serialization within the InitializeComponent(), all you need is the DesignerSerializationVisibilityAttribute:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
bitbonk
  • 45,662
  • 32
  • 173
  • 270