Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expose the Text property of a UserControl? [duplicate]

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
like image 599
Ian Boyd Avatar asked Nov 18 '11 19:11

Ian Boyd


1 Answers

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);
}
like image 112
SLaks Avatar answered Nov 09 '22 19:11

SLaks