7

Possible Duplicate:
Text property in a UserControl in C#

How do I mark the Text property of a UserControl as browsable?


A .NET UserControl class has a Text property.

Unfortunately the Text property of a UserControl isn't browsable:

//
//
// Returns:
//     The text associated with this control.
[Bindable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override string Text { get; set; }

In my UserControl I want to expose the Text property (i.e. make it "browsable") in the properties window. I tried blindly declaring it browsable:

[Browsable(true)]
public override string Text { get; set; }

and now it appears in the properties window, except now it does nothing.

I tried blindly calling base.Text to bring back the functionality:

[Browsable(true)]
public override string Text { get {return base.Text;} set { base.Text = value; this.Invalidate(); } }

and now the property does function at design-time, but the property value is not persisted to the Form.Designer.cs and it's InitalizeComponent code.

What is the proper way to expose the UserControl Text property so that it:

  • is browsable in the properties window
  • is functional
  • is persisted in the form designer

and, as a bonus:

  • know when it changes
1
  • 1
    Not quite an exact duplicate. My question was how (i.e. the proper way, as opposed to my random keyboard mashing) to make a property browsable. The other question is a property that already is browsable, but isn't being serialized.
    – Ian Boyd
    Nov 29, 2011 at 19:53

1 Answer 1

11

You're on the right track; just add [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

To find out when it changes, override OnTextChanged:

protected override void OnTextChanged (EventArgs eventArgs)
{
    System.Diagnostics.Trace.WriteLine("OnTextChanged(): eventArgs: " + eventArgs);  
    base.OnTextChanged(eventArgs);
}
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.