views:

95

answers:

3

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?

+3  A: 

You need more attributes:

[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Bindable(true)]
public override string Text 
ho1
It worked perfectly. Thank you.
yeyeyerman
A: 

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

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
bitbonk
+3  A: 

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 free, download it from redgate.com

Hans Passant
Hans, +1 for the advice. However I have to accept Ho answer, as it is also correct and it was written sooner. Thanks!
yeyeyerman