How to apply propertygrid's attributes to existing object?

G

Guest

I have assigned an existing object to propertygrid.selectedobject property.
But I only want some of the object's properties not browsable and/or
readonly. How can I do that?

I'm trying to find information about propertygrid but most of the
information seems to be for VS2003.
 
C

Chris Dunaway

I have assigned an existing object to propertygrid.selectedobject property.
But I only want some of the object's properties not browsable and/or
readonly. How can I do that?

I'm trying to find information about propertygrid but most of the
information seems to be for VS2003.

Check out the BrowsableAttribute class. You can decorate your
properties and methods with it to indicate which ones you want visible
in the property grid.

Another way would be to create an adapter object that contains an
instance of the "real" object and only expose the properties that you
need.

Chris
 
G

Guest

Hi Chris,

I think the BrowsableAttribute class is only useful if I have control on the
class coding. The object assigned to propertygrid is an object returned by
a .NET method so I have no direct way to change the attribute. Based on
what I have found so far, I think it is possible to change the attribute of
the existing object using reflection but I need to learn reflection first if
that is the only way.

Can you elaborate about the adapter object way?
 
V

VisualHint

Hello Peter,

On the PropertyGrid Resource List (http://
www.propertygridresourcelist.com), you will find a lot of
informations. One of the links of the list points to a Microsoft
article where the TypeDescriptionProvider (new in .net 2.0) is
explained. From the article:

"This allows you to write a separate class that implements
ICustomTypeDescriptor and then to register this class as the provider
of descriptions for other types".

This is here: http://msdn.microsoft.com/msdnmag/issues/05/05/NETMatters/

I hope this helps.

Best regards,

Nicolas Cadilhac @ VisualHint (http://www.visualhint.com)
Home of Smart FieldPackEditor.Net / DateTimePicker replacement (http://
www.visualhint.com/index.php/fieldpackeditor)
Home of Smart PropertyGrid for .Net and MFC (http://www.visualhint.com/
index.php/propertygrid)
Microsoft PropertyGrid Resource List - http://www.propertygridresourcelist.com
 
C

Chris Dunaway

Hi Chris,

I think the BrowsableAttribute class is only useful if I have control on the
class coding. The object assigned to propertygrid is an object returned by
a .NET method so I have no direct way to change the attribute. Based on
what I have found so far, I think it is possible to change the attribute of
the existing object using reflection but I need to learn reflection first if
that is the only way.

Can you elaborate about the adapter object way?

I simply meant create another class that acts as a container to the
class you want to limit the properties for and then only expose the
properties you want:

Public Class AdapaterClass

'classToLimit has lots of properties, but we only want some of
them
'visible in the PropertyGrid

Private _instance As classToLimit

Public Sub New(object As classToLimit )
_instance = object
End Sub

Public Property Property1() As String
Get
Return _instance.Property1
End Get
Set
_instance.Property1 = Value
End Set
End Property

Public Property Property2() As String
Get
Return _instance.Property2
End Get
Set
_instance.Property2 = Value
End Set
End Property

<Browsable(False)> _
Public ReadOnly Property Instance() As classToLimit
Get
Return _instance
End Get
End Property

End Class

Then you add an instance of the AdapterClass to the PropertyGrid.

I think VisualHint's suggestion is probably the way to go, but this
method, at least, is easy.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top