I have a user control that inherits from UserControl. It's a button so I'm trying to make the text in the button, change-able by using the Text property like the real buttons, instead of naming my own like _Text. I have the following code but it doesn't work (ie it doesn't show up in the Property Window). The name of the label is ContentPresenter
public override string Text
{
get
{
return ContentPresenter.Text;
}
set
{
ContentPresenter.Text = value;
}
}
If the Properties window is not visible, open it by pressing F4. Open the MyToolWindow window. You can find it in View > Other Windows. The window opens and the public properties of the window pane appear in the Properties window.
You can find Properties Window on the View menu. You can also open it by pressing F4 or by typing Properties in the search box. The Properties window displays different types of editing fields, depending on the needs of a particular property.
Remarks. Use the Text property to specify or determine the text content of the Label control. This property is commonly used to programmatically customize the text that is displayed in the Label control.
In most cases you will edit the Text to change the text that an object displays. You can do this by selecting the property in the Property window or by typing while the object is selected - Visual Studio assumes that you want to change the text property in this case.
UserControl goes to significant effort to hide the Text property. From the metadata:
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorBrowsable(EditorBrowsableState.Never)]
[Bindable(false)]
public override string Text { get; set; }
You can make it visible by overriding those attributes in your code:
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[EditorBrowsable(EditorBrowsableState.Always)]
[Bindable(true)]
public override string Text
{
get { return ContentPresenter.Text; }
set { ContentPresenter.Text = value; }
}
I'm not promising that's enough to make it work, but it probably is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With