SuperPeanut SuperPeanut - 3 months ago 37
Vb.net Question

What's the interest of PropertyGrid.BrowsableAttributes?


NB : This question is tagged C# too as it is a general question and an
answer describing the use of this in C# is perfectly fine to me.


I have been exploring the PropertyGrid lately in the .Net Framework. I have checked on this property (BrowsableAttributes) and I have no idea of the use of it.

At first I thought this would be able to loop through every
BrowsableAttribute
in your
SelectedObject
then you would be able to find back the original property, that would have been usefull.

But no, apparently all this property does is giving you an
AttributeCollection
containing only
BrowsableAttribute
, all set to
True
...

Can someone enlighten me on what is the point for such method ? I don't even understand how it's usefull inside the .NET...

Dim attributes = MyPropertyGrid.BrowsableAttributes
For Each A As Attribute In attributes
Dim Browsable As BrowsableAttribute = CType(A, BrowsableAttribute)
'Then how can I use this ? it's only property is Browsable (True/False)
Next


I was originally trying to solve a problem, where I don't know which object is selected in the property grid but I want to gather the object's data.

I have no idea on what the object's type is because it comes from a dynamically loaded DLL. I only know it is a derived class of another one, which I know. But I'm interesting in backing up the Object's properties obtained from the Property Grid to be able to save and load them later on.

Since the property grid already contains all those values, I thought this kind of property could be a shorcut for writing more code. I don't want to use reflection to inspect the code while the Property Grid already did it.

Answer

The PropertyGrid iterates over all properties of an object, using an approach similar to this answer. While it does it, it checks all attributes associated with that property and checks if it can find a match with the AttributeCollection (sample code, how this can be done is here). If it can find it, it will show up in the property grid, otherwise not.

The purpose is, that you can hide public properties to show up in a property grid by either mark it with [Browsable(false)] if you need a blacklist support. If you like a whitelist approach, you can define your own Attribute, apply it to all desired properties and set the BrowsableAttribute to a collection containing your own attribute only.