Click here to Skip to main content
15,886,518 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
Imports System.Windows.Forms
Imports System.Windows.Forms.Design

Namespace PropertyGridEx
    <Serializable()> _
    Public Class CustomChoices
        Inherits ArrayList

        Public Sub New(ByVal array As ArrayList, ByVal IsSorted As Boolean)
            Me.AddRange(array)
            If IsSorted Then Me.Sort()
        End Sub

        Public Sub New(ByVal array As ArrayList)
            Me.AddRange(array)
        End Sub

        Public Sub New(ByVal array() As String, ByVal IsSorted As Boolean)
            Me.AddRange(array)
            If IsSorted Then Me.Sort()
        End Sub

        Public Sub New(ByVal array() As String)
            Me.AddRange(array)
        End Sub

        Public Sub New(ByVal array() As Integer, ByVal IsSorted As Boolean)
            Me.AddRange(array)
            If IsSorted Then Me.Sort()
        End Sub

        Public Sub New(ByVal array() As Integer)
            Me.AddRange(array)
        End Sub

        Public Sub New(ByVal array() As Double, ByVal IsSorted As Boolean)
            Me.AddRange(array)
            If IsSorted Then Me.Sort()
        End Sub

        Public Sub New(ByVal array() As Double)
            Me.AddRange(array)
        End Sub

        Public Sub New(ByVal array() As Object, ByVal IsSorted As Boolean)
            Me.AddRange(array)
            If IsSorted Then Me.Sort()
        End Sub

        Public Sub New(ByVal array() As Object)
            Me.AddRange(array)
        End Sub

        Public ReadOnly Property Items() As ArrayList
            Get
                Return Me
            End Get
        End Property

        Public Class CustomChoicesTypeConverter
            Inherits TypeConverter
            Private oChoices As CustomChoicesAttributeList = Nothing
            Public Overloads Function GetStandardValuesSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
                Dim Choices As CustomChoicesAttributeList = context.PropertyDescriptor.Attributes(GetType(CustomChoicesAttributeList))
                If Not oChoices Is Nothing Then Return True
                If Not Choices Is Nothing Then
                    oChoices = Choices
                    GetStandardValuesSupported = True
                Else
                    GetStandardValuesSupported = False
                End If
            End Function
            Public Overloads Function GetStandardValuesExclusive(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
                Dim Choices As CustomChoicesAttributeList = context.PropertyDescriptor.Attributes(GetType(CustomChoicesAttributeList))
                If Not oChoices Is Nothing Then Return True
                If Not Choices Is Nothing Then
                    oChoices = Choices
                    GetStandardValuesExclusive = True
                Else
                    GetStandardValuesExclusive = False
                End If
            End Function
            Public Overloads Function GetStandardValues(ByVal context As System.ComponentModel.ITypeDescriptorContext) As System.ComponentModel.TypeConverter.StandardValuesCollection
                Dim Choices As CustomChoicesAttributeList = context.PropertyDescriptor.Attributes(GetType(CustomChoicesAttributeList))
                If Not oChoices Is Nothing Then
                    Return oChoices.Values
                End If
                Return MyBase.GetStandardValues(context)
            End Function
        End Class

        Public Class CustomChoicesAttributeList
            Inherits Attribute
            Private oList As New ArrayList

            Public ReadOnly Property Item() As ArrayList
                Get
                    Return Me.oList
                End Get
            End Property

            Public ReadOnly Property Values() As TypeConverter.StandardValuesCollection
                Get
                    Return New TypeConverter.StandardValuesCollection(Me.oList)
                End Get
            End Property

            Public Sub New(ByVal List() As String)
                MyBase.New()
                oList.AddRange(List)
            End Sub

            Public Sub New(ByVal List As ArrayList)
                MyBase.New()
                oList.AddRange(List)
            End Sub

            Public Sub New(ByVal List As ListBox.ObjectCollection)
                MyBase.New()
                oList.AddRange(List)
            End Sub
        End Class
    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