Click here to Skip to main content
15,881,588 members
Articles / Programming Languages / C#
Tip/Trick

Making an overridden Text property visible in the Visual Studio designer for a UserControl

Rate me:
Please Sign up or sign in to vote.
5.00/5 (8 votes)
14 Jun 2012CPOL 25.5K   3   4
Having the property "Text" be available for almost everything in .NET that interacts with the user makes life very simple. But when you create a new UserControl, the Text property doesn't appear in the Properties pane.

Introduction

When you derive a new control from UserControl, you can happily get and set the Text property from your code - Intellisense finds it, it is a string, it works exactly as you expect.

Except... it doesn't appear in the Properties pane for your control. So your derived control instances can't set individual Text values at design time. Annoying.

Background 

You can try creating a Text property:

C#
public string Text { get; set; }

...but the compiler complains it already exists, and suggests you try "new" or "override".  You can try overriding the Text property:

C#
public override string Text { get; set; }

...and it works fine. Except it doesn't show in the designer.

You can try creating a new Text property:

C#
public new string Text { get; set; }

... and it works fine, too. Still can't see it in the designer though. You can try setting the EditorBrowserVisibility:

C#
[EditorBrowsable(EditorBrowsableState.Always)]
public override string Text { get; set; }

...but that doesn't work either.  

Showing the Text Property in the designer  

C#
[EditorBrowsable(EditorBrowsableState.Always), Browsable(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text {get; set;} 

Annoying? Yes.

Obvious? No.

Fixed? Yes.

History

Original version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

 
QuestionThank you very much for this tip... Pin
SungHwan Park1-Dec-16 12:45
SungHwan Park1-Dec-16 12:45 
QuestionNice and concise Pin
SBendBuckeye16-Jun-15 3:18
SBendBuckeye16-Jun-15 3:18 
GeneralThanks Pin
Ashraf Sabry19-Jan-13 8:58
Ashraf Sabry19-Jan-13 8:58 
GeneralSimple but... Pin
db7uk14-Jun-12 5:19
db7uk14-Jun-12 5:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.