Click here to Skip to main content
15,895,667 members
Articles / Desktop Programming / Windows Forms

Add Custom Properties to a PropertyGrid

Rate me:
Please Sign up or sign in to vote.
4.94/5 (219 votes)
22 Aug 2006Apache6 min read 1.6M   27.8K   443  
Extend a PropertyGrid with an Item collection; easy customization of properties with custom editor, custom converter and databinding.
Imports System.ComponentModel

Namespace PropertyGridEx
    Public Class BrowsableTypeConverter

        Inherits ExpandableObjectConverter

        Public Enum LabelStyle
            lsNormal
            lsTypeName
            lsEllipsis
        End Enum

        Public Class BrowsableLabelStyleAttribute
            Inherits Attribute
            Private eLabelStyle As LabelStyle = LabelStyle.lsEllipsis
            Public Sub New(ByVal LabelStyle As LabelStyle)
                eLabelStyle = LabelStyle
            End Sub
            Public Property LabelStyle() As LabelStyle
                Get
                    Return eLabelStyle
                End Get
                Set(ByVal value As LabelStyle)
                    eLabelStyle = value
                End Set
            End Property
        End Class

        Public Overloads Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As System.Type) As Boolean
            Return True
        End Function

        Public Overloads Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As System.Type) As Object
            Dim Style As BrowsableLabelStyleAttribute = context.PropertyDescriptor.Attributes(GetType(BrowsableLabelStyleAttribute))
            If Not Style Is Nothing Then
                Select Case Style.LabelStyle
                    Case LabelStyle.lsNormal
                        Return MyBase.ConvertTo(context, culture, value, destinationType)
                    Case LabelStyle.lsTypeName
                        Return "(" & value.GetType.Name & ")"
                    Case LabelStyle.lsEllipsis
                        Return "(...)"
                End Select
            End If
            Return MyBase.ConvertTo(context, culture, value, destinationType)
        End Function

    End Class
End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Software Developer (Senior)
Italy Italy
I am 40 years old and I've been working with C++, Visual Basic .NET, C# and ASP.NET. I have a large experience in Industrial Automation solutions, but I've worked also as Web developer and DBA. I like to share knowledge and projects with other people.

Comments and Discussions