Name property of UserControl or Control
-
06-07-2019 - |
Question
What is special about the Name property of a Control or UserControl that causes it to be displayed as "(Name)
" inside the property grid within Visual Studio?
Solution
Check out this article about design-time attributes in .NET. Specifically, I think you are looking for the Browsable
attribute, which enables properties in Visual Studio's design-time properties dialogue.
If you have a property called Name
, you'd declare it like this:
[Browsable(true)]
public string Name { /*...*/ }
You can set a lot more attributes, like Description
, DefaultValue
and Category
, which will come in handy if you're planning on presenting your controls to other developers.
EDIT: To get the effect that you want, use both the Browsable
and ParenthesizePropertyName
attributes:
[Browsable(true)]
[ParenthesizePropertyName(true)]
public string Name { /*...*/ }
(Thanks to Ksempac from the comments for this.)
Since you didn't specify if you're using VB or C#, here's the same thing in VB:
<Browsable(true)> _
<ParenthesizePropertyName(true)> _
Public Property Name(Value As String) As String
' ...
End Property
EDIT 2:
I think you're wondering about why you would want to surround your property with parentheses in the first place, or perhaps what it means for a property's name to have parentheses around it.
You can find the answer to that here:
Parenthesized properties are shown at the top of the window — or at the top of their category if the list is grouped by category
Basically, if a property is important, you want it to appear at the top of a sorted list, so you surround it with parentheses to indicate this.